1

According to apple documentation pinching is a continuous gesture & mean to be use with two fingers. Even with three finger pinching it seems working fine in swift 4.1.

I tried to print number of touches. Even with three fingers it gives number of touches as 2. It seems it detects first 2 finger touches and ignore third.So no way to filter.

@objc func pinch(pinchGesture: UIPinchGestureRecognizer){
  if pinchGesture.state == UIGestureRecognizerState.began {
    print("number of touches:\(pinchGesture.numberOfTouches)")
  }
}

I am calling setupGesture() method in viewDidLoad. So it handles relevant user pinch gestures.

func setupGestures(){

        let pinch = UIPinchGestureRecognizer(target: self, action: #selector(self.pinch))
        pinch.delegate = self
        self.addGestureRecognizer(pinch)

    }

Is there any possible way to avoid three finger pinching in ios?

2 Answers2

1

You can filter touches in:

func touchesBegan(_ touches: Set<UITouch>, 
         with event: UIEvent)

For example get number of touches from argument touches and if touches.count > 2 then don't act on this gesture - ignore it.

EDIT(1):

After your edit I have another idea (I don't have Mac with me, so I can't try this right now). Try to use this method from delegate. You should be able to get touch before accepting. I don't see information if this will be called once, or one for each touch (each finger). Maybe you can use it in some way?

franiis
  • 1,378
  • 1
  • 18
  • 33
  • Btw I am using gesture recognizers (UIPinchGestureRecognizer) not the touch event .I tried what you sugest earlier. But even with 3 fingers, number of touches ( pinchGesture.numberOfTouches) shows as 2. That means it consider first two fingers and ignore the third fingure touch. So it seems it works for three finger touch. – Kasun Palihakkara Aug 14 '18 at 05:55
  • @user8028778 Please try my edit. I can't test it right now, but maybe it will help. – franiis Aug 14 '18 at 06:41
  • I tried to use this delegate method too. But it's called before touch begin(as declared in apple documentation). So gestureRecognizer.numberOfTouches is always zero as it is called before touch begin. – Kasun Palihakkara Aug 14 '18 at 06:47
  • If so, you maybe need to subclass `UIPinchGestureRecognizer` and change logic in `touchesBegin`. I guess changes would be minimal and it shouldn't be hard. I can try to test it in few hours if you can't to solve it on your own. – franiis Aug 14 '18 at 06:55
  • Look here: https://stackoverflow.com/questions/13982409/three-finger-pinch-gesture – franiis Aug 14 '18 at 06:55
  • I figured out a way. Your sugestions helps.Thanks in advanced. – Kasun Palihakkara Aug 14 '18 at 08:33
0

Lately I figured out an approach to this problem.This might not be the best solution or can be alternatives. But it works.

Here I just keep an boolean

 var numberOfAllTouchesLessThanOrEqualsToTwo = false

I used event handler to set boolean value.

   override func touchesBegan(_ touches: Set<UITouch>,with event: UIEvent?){

            if (event?.allTouches?.count)! > 2 {
                numberOfAllTouchesLessThanOrEqualsToTwo = false
            }else{
                numberOfAllTouchesLessThanOrEqualsToTwo = true
            }

        }

    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {

            if (event?.allTouches?.count)! > 2 {
                numberOfAllTouchesLessThanOrEqualsToTwo = false
            }else{
                numberOfAllTouchesLessThanOrEqualsToTwo = true
            }

        }

    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {

            if (event?.allTouches?.count)! > 2 {
                numberOfAllTouchesLessThanOrEqualsToTwo = false
            }else{
                numberOfAllTouchesLessThanOrEqualsToTwo = true
            }
        }

and I used this boolean inside gesture recognizer.

@objc func pinch(pinchGesture: UIPinchGestureRecognizer){

     if pinchGesture.state == UIGestureRecognizerState.changed {
          if numberOfAllTouchesLessThanOrEqualsToTwo {
            //code
          }
      }
 }

It works.