Snapkit似乎阻止了子视图上的所有GestureRecognizer

时间:2016-01-31 02:37:08

标签: ios swift swift2 uitapgesturerecognizer

我已经创建了一个自定义的UIView,它在自己的init中添加了一个UITapGestureRecognizer:

class BoxView: UIView, UIGestureRecognizerDelegate {
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.construct()
    }
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)

        self.construct()
    }
    convenience init() {
        self.init(frame: CGRect.zero)
    }

    func construct() {
        self.backgroundColor = UIColor.whiteColor()
        self.frame.size = CGSize(width: 125, height: 125)
        let tapGesture = UITapGestureRecognizer(target: self, action: Selector("handleTap:"))
        tapGesture.delegate = self
        self.addGestureRecognizer(tapGesture)
    }

    func handleTap(sender: UITapGestureRecognizer? = nil) {
        print("handleTap called!")
    }
} 

我在ViewController中使用它,如下所示:     进口基金会     导入UIKit     导入AVFoundation

class StartViewController: UIViewController {
    private var boxView: BoxView = BoxView()

    override func viewDidLoad() {
        super.viewDidLoad()

        self.view.backgroundColor = UIColor.blackColor()
        self.view.addSubview(self.boxView)

        /* block: XXX
        self.boxView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self.view)
        }
        */

    }
}

在我取消注释ViewController中标记为“XXX”的块时,手势似乎不起作用,该块添加了SnapKit约束。有人能帮我理解这里发生了什么吗?我正确使用手势识别器吗?

1 个答案:

答案 0 :(得分:0)

看起来我错过了一个重要的约束,添加一个" size"自定义视图(BoxView)上的约束修复了它。

func construct() {
    self.backgroundColor = UIColor.whiteColor()
    self.frame.size = CGSize(width: 125, height: 125)

    self.snp_makeConstraints { (make) -> Void in
        make.size.equalTo(self.frame.size)
    }

    let tapGesture = UITapGestureRecognizer(target: self, action: Selector("handleTap:"))
    tapGesture.delegate = self
    self.addGestureRecognizer(tapGesture)
}