Ios Snapkit似乎正在阻止子视图上的所有手势识别器
我创建了一个自定义UIView,它在自己的init中添加了一个UIApgestureRecognitizer:Ios Snapkit似乎正在阻止子视图上的所有手势识别器,ios,swift,swift2,uitapgesturerecognizer,Ios,Swift,Swift2,Uitapgesturerecognizer,我创建了一个自定义UIView,它在自己的init中添加了一个UIApgestureRecognitizer: class BoxView: UIView, UIGestureRecognizerDelegate { override init(frame: CGRect) { super.init(frame: frame) self.construct() } required init?(coder aDecoder: NSCoder
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
进口AVF基金会
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)
}
*/
}
}
只要我在添加SnapKit约束的ViewController中取消注释标记为“XXX”的块,这些手势似乎就不起作用。有人能帮我理解这里发生了什么吗?我是否正确使用手势识别器 看起来我错过了一个重要的约束,在自定义视图(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)
}