Reputation: 321
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?
Upvotes: 1
Views: 607
Reputation: 321
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.
Upvotes: 0
Reputation: 1376
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?
Upvotes: 1