Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect pinch in gesture vs pinch out gesture?

Instead of scaling, which I think pinch gesture is usually used for, I am looking to just detect whether the pinch was a pinch in vs pinch out so I can collapse or expand some table sections. How would I go about doing that?

like image 555
user1337645 Avatar asked Jun 24 '12 13:06

user1337645


3 Answers

The "scale" property is less than 1 for pinch-in gesture and greater than 1 for pinch-out. This happens for all pinches with 2 fingers.

What I also observed was that if I pinched in with 5 fingers (which is the shortcut to minimize-to-home), the scale value comes exactly 1.0 - everytime. But this is not supported by any Apple documentation that I'm aware of.

You can experiment what the values are coming by simply putting an NSLog in your pinch handling selector

NSLog(@"Scale: %.2f | Velocity: %.2f",pinch.scale,pinch.velocity);
like image 159
ScorpionKing2k5 Avatar answered Sep 22 '22 18:09

ScorpionKing2k5


Well, that seems an easy one. The UIPinchGestureRecognizer class has only two properties, scale and velocity. It seems logical that a negative scale would mean an inward pinch, a positive scale an outward pinch.

NB: "negative" might be misleading. "Smaller" is 0.0 < scale < 1.0, "bigger" is scale > 1.0.

like image 20
Mundi Avatar answered Sep 23 '22 18:09

Mundi


You were right to look at the scale property however it switches around 1, not zero.

    - (BOOL) pinchWasOutwards:(UIGestureRecognizer *)gestureRecognizer
    {
        return gestureRecognizer.scale > 1;
    }
like image 33
Jon Deokule Avatar answered Sep 26 '22 18:09

Jon Deokule