7

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?

vikingosegundo
  • 52,040
  • 14
  • 137
  • 178
user1337645
  • 473
  • 8
  • 16
  • It is far better to use .velocity, which (obviously!) is positive or negative depending on which way you are going. The answers below are whacky. – Fattie Jul 11 '17 at 12:17

3 Answers3

11

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);
ScorpionKing2k5
  • 2,491
  • 1
  • 18
  • 18
8

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.

Mundi
  • 79,884
  • 17
  • 117
  • 140
  • hmm pinching out also gives me negative scale? fyi im using pinch dragged from storyboard, with scale set as 1 and delayed end checked and delayed begin unchecked – user1337645 Jun 24 '12 at 14:06
  • 1
    Impossible. I doubt that pinching out gives you a negative scale. Please check and describe how you get to this result. – Mundi Jun 24 '12 at 22:41
  • 3
    pinching inward results in a scale that starts at 1.0 and decreases to 0.0, and pinching outward results in a scale that starts and 1.0 and increases from there. – Scott Allen May 22 '13 at 21:54
3

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

    - (BOOL) pinchWasOutwards:(UIGestureRecognizer *)gestureRecognizer
    {
        return gestureRecognizer.scale > 1;
    }
Jon Deokule
  • 1,506
  • 2
  • 12
  • 7