I have found no way in the documentation on how to specify the number of touches for UIPinchGestureRecognizer or UIRotationGestureRecognizer. All I found anywhere is that it only works with two fingers, but by my experiments, it also works with 3 or even more fingers. Furthermore in the action the property numberOfTouches also never returns the actual number of fingers. I want to limit it only for two fingers because it gets all confused with other 3-finger recognizers. Could you, please, suggest me a good way to do that? Thanks.
2 Answers
According to the docs UIPinchGestureRecognizer
handles
[...] pinching gestures involving two touches [...]
Apparently it only considers two touches but allows additional touches to happen concurrently.
To answer your question: you can try to get the actual number of touches by other means and prevent the pinch action when that count is larger than 2. One way is to add more gesture recognizers which handle gestures on the same view (e.g. multiple UITapGestureRecognizer
s, one for each possible number of touches); another one is to override touchesBegan
and touchesMoved
of the view your gesture recognizer is installed on and use the count of the provided touches
array.
(I'd go with the second approach first because managing multiple gesture recognizers in parallel can get problematic.)

- 5,723
- 3
- 26
- 47
-
with approach #2, if you decide you want to cancel the pinch gesture because you detect three touches, how would you cancel the pinch gesture? – Crashalot Mar 30 '18 at 06:51
-
@Crashalot again, two ideas come to mind... 1. store the current number of touches (by means of `touchesBegan`/`touchesMoved`) in a member var of the view controller, and `guard` against invalid counts in the gesture recognizer action; or 2. only install the recognizer on the view when you have the correct number of touches (again, via `touchesBegan`/`touchesMoved`), and uninstall it when the touch count is wrong (which includes no touches at all). Disclaimer: I have never used the 2nd approach, but I think it should work. – dr_barto Mar 30 '18 at 07:48
Add a delegate to the pinch gesture recogniser you're concerned about.
Implement gestureRecognizer(_:, shouldRecognizeSimultaneouslyWith:)
and return false
if you want the pinch gesture to be ignored if there is another recogniser also in progress.

- 118,105
- 32
- 252
- 268