为什么我的swift协议不起作用?

时间:2015-12-04 07:10:29

标签: ios uibutton swift2 protocols

我试图使用协议从Button类调用ViewController中的函数,但它不起作用。基本上我将我的按钮子类化并使用override func touchesBegan。当我触摸我的按钮时,它会起作用,并说“ touchesBegan来自UIButton Subsclass ”但问题是为什么我在视图控制器上放置的func didTapOnItem没有反应什么?这是我的代码:

protocol AnimationProtocol {
    func didTapOnItem(touches: Set<UITouch>)
}
class Buttons: UIButton {

    @IBInspectable var fillColor: UIColor = UIColor.greenColor()
    @IBInspectable var isAddButton: Bool = true

    var delegate: AnimationProtocol?

    let effectTwo = ButtonEffectTwo(frame: CGRectZero)

    override func drawRect(rect: CGRect) {

        let path = UIBezierPath(ovalInRect: rect)
        fillColor.setFill()
        path.fill()

    }

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        print("touchesBegan from UIButton Subsclass")
        super.touchesBegan(touches, withEvent: event)
        self.delegate?.didTapOnItem(touches)


    }


}

下面的这个函数我把我的ViewController类放在了:

class ViewController: UIViewController, AnimationProtocol {
      func didTapOnItem(touches: Set<UITouch>) {
                print("touchesBegan from didtap")

                view.addSubview(effectTwo)
            }
}

任何人都可以帮助我吗?

0 个答案:

没有答案