iosobjective-cuipinchgesturerecognizer

limit UIPinchGestureRecognizer to work with exactly two fingers only


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.


Solution

  • 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 UITapGestureRecognizers, 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.)