没有调用Subview Gesture Recognizer

时间:2016-06-26 22:37:47

标签: ios iphone swift uiview uigesturerecognizer

我在自定义UIView.to中正确运行UITapGestureRecognizer时遇到问题。我创建了一个视图:CategoryViewButton,它在init中添加了一个UITapGestureRecognizer:

class CategoryViewButton: UIView {

override init(frame: CGRect) {
    super.init(frame: frame)
    let tapGesture = UITapGestureRecognizer(target: self, action: #selector(self.handleTap))
    self.addGestureRecognizer(tapGesture)
 }

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

func handleTap() {
    print("Hello again")
 }
}

直接在视图控制器中添加时,此手势识别器可以正常工作。但是,当我将CategoryViewButton添加为另一个自定义视图的子视图时,不会调用手势识别器方法。我的子视图:

class CategoryView: UIView, CategoryButtonDelegate {

var button : CategoryViewButton?

override init(frame: CGRect) {
    super.init(frame: frame)
    button = CategoryViewButton(frame: CGRect(x: 10, y: 0, width: 40, height: 25))
    self.addSubview(button!)
    self.bringSubview(toFront: button!)
}

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

当我在View Controller中创建一个CategoryView时,不会调用handleTap()函数。我错过了什么?

1 个答案:

答案 0 :(得分:6)

对于任何好奇的人来说,问题是带有手势识别器的子视图超出了超视图的框架。这意味着即使正在绘制视图,也未检测到手势