我有一个红色视图和一个绿色视图(绿色视图是redview的子视图),并且向每个视图添加了一个actionRecognizer。
并且两个grestureRecognizer的shouldRecognizeSimultaneouslyWith
都返回YES。
但是,当我点击绿色视图时,将首先调用redview的手势委托方法shouldRecognizeSimultaneouslyWith
。
因为我认为击球测试视图应该是绿色视图。因此,绿色视图的shouldRecognizeSimultaneouslyWith
应该是第一个。
iOS如何确定应该首先重新设置哪个grestureRecognizer?
发布代码并打印日志:
class GreenView: UIView,UIGestureRecognizerDelegate{
override func awakeFromNib() {
let gesture = UITapGestureRecognizer(target: self, action: #selector(greenTap))
gesture.delegate = self
self.addGestureRecognizer(gesture)
}
@objc func greenTap(){
print("greenTap")
}
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
print("greenTap shouldRecognizeSimultaneouslyWith")
return true
}
override func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
print("green gestureRecognizerShouldBegin")
return super.gestureRecognizerShouldBegin(gestureRecognizer)
}
}
class RedView: UIView,UIGestureRecognizerDelegate{
override func awakeFromNib() {
let gesture = UITapGestureRecognizer(target: self, action: #selector(redTap))
gesture.delegate = self
self.addGestureRecognizer(gesture)
}
@objc func redTap(){
print("redTap")
}
override func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
print("redTap gestureRecognizerShouldBegin")
return super.gestureRecognizerShouldBegin(gestureRecognizer)
}
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
print("redTap shouldRecognizeSimultaneouslyWith")
return true
}
}
和印刷品是:
green gestureRecognizerShouldBegin
redTap gestureRecognizerShouldBegin
green gestureRecognizerShouldBegin
redTap shouldRecognizeSimultaneouslyWith
greenTap shouldRecognizeSimultaneouslyWith
redTap
greenTap
我们可以看到redview(superview)的shouldRecognizeWithWith和action方法在greenview(subview)的方法之前被调用
最佳答案
不要将绿色视图添加为红色视图的子视图。只需在其上方的红色视图之外添加绿色视图,并将约束集中到红色视图
关于ios - UIGestureRecognizer识别订单问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58990614/