在GooeySlideMenu上添加UITableView委托方法时不会调用它们

时间:2017-04-24 10:29:02

标签: ios iphone uitableview swift3

我想在UIView之上添加UITableView,UIView是GooeySlideMenu的子类。如在git中显示的示例中,我创建了UITableView而不是UIButtons,但是没有调用委托方法。

以下是我的参考代码:

class GooeySlideMenu: UIView {

fileprivate var _option: MenuOptions
fileprivate var keyWindow: UIWindow?
fileprivate var blurView: UIVisualEffectView!
fileprivate var helperSideView: UIView!
fileprivate var helperCenterView: UIView!

fileprivate var diff: CGFloat = 0.0
fileprivate var triggered: Bool = false
fileprivate var displayLink: CADisplayLink?
fileprivate var animationCount: Int = 0
fileprivate   var myTableView: tableViewCustomClass  =  tableViewCustomClass()


init(options: MenuOptions) {
    _option = options
    if let kWindow = UIApplication.shared.keyWindow{
        keyWindow = kWindow
        let frame = CGRect(
            x: -kWindow.frame.size.width/2 - options.menuBlankWidth,
            y: 0,
            width: kWindow.frame.size.width/2 + options.menuBlankWidth,
            height: kWindow.frame.size.height)
        super.init(frame:frame)
    } else {
        super.init(frame:CGRect.zero)
    }
    setUpViews()
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

override func draw(_ rect: CGRect) {
    let path = UIBezierPath()
    path.move(to: CGPoint(x: 0, y: 0))
    path.addLine(to: CGPoint(x: frame.width-_option.menuBlankWidth, y: 0))
    path.addQuadCurve(to: CGPoint(x: frame.width-_option.menuBlankWidth, y: frame.height), controlPoint: CGPoint(x: frame.width-_option.menuBlankWidth+diff, y: frame.height/2))
    path.addLine(to: CGPoint(x: 0, y: frame.height))
    path.close()

    let context = UIGraphicsGetCurrentContext()
    context?.addPath(path.cgPath)
    _option.menuColor.set()
    context?.fillPath()
}

func trigger() {
    if !triggered {
        if let keyWindow = keyWindow {
            keyWindow.insertSubview(blurView, belowSubview: self)
            UIView.animate(withDuration: 0.3, animations: { [weak self] () -> Void in
                self?.frame = CGRect(
                    x: 0,
                    y: 0,
                    width: keyWindow.frame.size.width/2 + (self?._option.menuBlankWidth)!,
                    height: keyWindow.frame.size.height)
            })

            beforeAnimation()
            UIView.animate(withDuration: 0.7, delay: 0.0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0.9, options: [.beginFromCurrentState,.allowUserInteraction], animations: { [weak self] () -> Void in
                self?.helperSideView.center = CGPoint(x: keyWindow.center.x, y: (self?.helperSideView.frame.size.height)!/2);
                }, completion: { [weak self] (finish) -> Void in
                    self?.finishAnimation()
                })

            UIView.animate(withDuration: 0.3, animations: { [weak self] () -> Void in
                self?.blurView.alpha = 1.0
            })

            beforeAnimation()
            UIView.animate(withDuration: 0.7, delay: 0.0, usingSpringWithDamping: 0.8, initialSpringVelocity: 2.0, options: [.beginFromCurrentState,.allowUserInteraction], animations: { [weak self] () -> Void in
                self?.helperCenterView.center = keyWindow.center
                }, completion: { [weak self] (finished) -> Void in
                    if finished {
                        let tapGesture = UITapGestureRecognizer(target: self, action: #selector(GooeySlideMenu.tapToUntrigger))
                        self?.blurView.addGestureRecognizer(tapGesture)
                        self?.finishAnimation()
                    }
            })
          //  animateButtons()
            myTableView.reloadData()
            triggered = true
        }
    } else {
        tapToUntrigger()
    }
}

}

 extension GooeySlideMenu {

fileprivate func setUpViews() {
    if let keyWindow = keyWindow {
        blurView = UIVisualEffectView(effect: UIBlurEffect(style: _option.blurStyle))
        blurView.frame = keyWindow.frame
        blurView.alpha = 0.0

        helperSideView = UIView(frame: CGRect(x: -40, y: 0, width: 40, height: 40))
        helperSideView.backgroundColor = UIColor.red
        helperSideView.isHidden = true
        keyWindow.addSubview(helperSideView)

        helperCenterView = UIView(frame: CGRect(x: -40, y: keyWindow.frame.height/2 - 20, width: 40, height: 40))
        helperCenterView.backgroundColor = UIColor.yellow
        helperCenterView.isHidden = true
        keyWindow.addSubview(helperCenterView)

        backgroundColor = UIColor.clear
        keyWindow.insertSubview(self, belowSubview: helperSideView)

        addUItableView()
      //  addButton()
    }
}

fileprivate func addUItableView(){



    myTableView.frame = CGRect(x: 0, y: 20, width: 300, height: 200)


    myTableView.backgroundColor = UIColor.white
    myTableView.delegate = tableViewCustomClass() as? UITableViewDelegate
    myTableView.dataSource = tableViewCustomClass() as? UITableViewDataSource
    addSubview(myTableView)

}

1 个答案:

答案 0 :(得分:1)

您需要在tableViewCustomClass的类而不是GooeySlideMenu类上声明tableview的委托。

在tableViewCustomClass

中 tableViewCustomClass的初始化方法

中的

self.delegate = self