iOS:识别捏合手势但不识别两指平移?
iOS: recognize pinch gestures but not two finger pans?
目标是仅在未进行双指平移时识别捏合手势。
此代码允许 pinchGesture
和双指 panGestures
,但它们同时出现。这是基于 使用 gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer: UIGestureRecognizer)
函数。
// Handle two-finger pans
let twoFingerPanRecognizer = UIPanGestureRecognizer(target: self, action: #selector(sceneViewPannedTwoFingers))
twoFingerPanRecognizer.minimumNumberOfTouches = 2
twoFingerPanRecognizer.maximumNumberOfTouches = 2
twoFingerPanRecognizer.delegate = self
sceneView.addGestureRecognizer(twoFingerPanRecognizer)
// Handle pinches
let pinchGesture = UIPinchGestureRecognizer(target: self, action: #selector(sceneViewPinched))
pinchGesture.delegate = self
sceneView.addGestureRecognizer(pinchGesture)
func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer: UIGestureRecognizer) -> Bool {
if gestureRecognizer is UIPinchGestureRecognizer {
return false
} else {
return true
}
}
此代码允许双指平移,但捏合根本不起作用。
// Handle two-finger pans
let twoFingerPanRecognizer = UIPanGestureRecognizer(target: self, action: #selector(sceneViewPannedTwoFingers))
twoFingerPanRecognizer.minimumNumberOfTouches = 2
twoFingerPanRecognizer.maximumNumberOfTouches = 2
twoFingerPanRecognizer.delegate = self
sceneView.addGestureRecognizer(twoFingerPanRecognizer)
// Handle taps
let tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(sceneViewTapped))
sceneView.addGestureRecognizer(tapRecognizer)
// Handle pinches
let pinchGesture = UIPinchGestureRecognizer(target: self, action: #selector(sceneViewPinched))
pinchGesture.delegate = self
pinchGesture.requireGestureRecognizerToFail(twoFingerPanRecognizer)
sceneView.addGestureRecognizer(pinchGesture)
func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}
你试过这样吗
func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool {
if ((gestureRecognizer is UIPinchGestureRecognizer || gestureRecognizer is UIPanGestureRecognizer)
&& (otherGestureRecognizer is UIPinchGestureRecognizer || otherGestureRecognizer is UIPanGestureRecognizer)) {
return false
}
return true
}
目标是仅在未进行双指平移时识别捏合手势。
此代码允许 pinchGesture
和双指 panGestures
,但它们同时出现。这是基于 gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer: UIGestureRecognizer)
函数。
// Handle two-finger pans
let twoFingerPanRecognizer = UIPanGestureRecognizer(target: self, action: #selector(sceneViewPannedTwoFingers))
twoFingerPanRecognizer.minimumNumberOfTouches = 2
twoFingerPanRecognizer.maximumNumberOfTouches = 2
twoFingerPanRecognizer.delegate = self
sceneView.addGestureRecognizer(twoFingerPanRecognizer)
// Handle pinches
let pinchGesture = UIPinchGestureRecognizer(target: self, action: #selector(sceneViewPinched))
pinchGesture.delegate = self
sceneView.addGestureRecognizer(pinchGesture)
func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer: UIGestureRecognizer) -> Bool {
if gestureRecognizer is UIPinchGestureRecognizer {
return false
} else {
return true
}
}
此代码允许双指平移,但捏合根本不起作用。
// Handle two-finger pans
let twoFingerPanRecognizer = UIPanGestureRecognizer(target: self, action: #selector(sceneViewPannedTwoFingers))
twoFingerPanRecognizer.minimumNumberOfTouches = 2
twoFingerPanRecognizer.maximumNumberOfTouches = 2
twoFingerPanRecognizer.delegate = self
sceneView.addGestureRecognizer(twoFingerPanRecognizer)
// Handle taps
let tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(sceneViewTapped))
sceneView.addGestureRecognizer(tapRecognizer)
// Handle pinches
let pinchGesture = UIPinchGestureRecognizer(target: self, action: #selector(sceneViewPinched))
pinchGesture.delegate = self
pinchGesture.requireGestureRecognizerToFail(twoFingerPanRecognizer)
sceneView.addGestureRecognizer(pinchGesture)
func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}
你试过这样吗
func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool {
if ((gestureRecognizer is UIPinchGestureRecognizer || gestureRecognizer is UIPanGestureRecognizer)
&& (otherGestureRecognizer is UIPinchGestureRecognizer || otherGestureRecognizer is UIPanGestureRecognizer)) {
return false
}
return true
}