我正在尝试启用同步手势识别,但仅适用于UIPinchGestureRecognizer
和UIRotationGestureRecognizer
手势。我不希望它适用于任何其他手势。如果我将以下属性设置为true,则允许同时识别所有手势,如何将其限制为仅旋转和缩放?
func gestureRecognizer(UIGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}
答案 0 :(得分:43)
确保您的班级实施UIGestureRecognizerDelegate
class YourViewController: UIViewController, UIGestureRecognizerDelegate ...
将手势delegate
设置为self
yourGesture.delegate = self
将代理功能添加到您的班级
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
if (gestureRecognizer is UIPanGestureRecognizer || gestureRecognizer is UIRotationGestureRecognizer) {
return true
} else {
return false
}
}
答案 1 :(得分:-1)
任何2美分,可快速获得5.1
//假设您需要捏一点来平移:
//UIGestureRecognizerDelegate
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith shouldRecognizeSimultaneouslyWithGestureRecognizer: UIGestureRecognizer) -> Bool {
if gestureRecognizer is UIPinchGestureRecognizer {
return true
}
return false
}