Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use UIPinchGestureRecognizers to distinguish between horizontal and vertical pinches?

I have a view which the user can pinch to grow or shrink. I'd like this to work along two axes - if the pinch is mostly horizontal, it will grow/shrink the object horizontally, but if the pinch is mostly vertical, it will grow/shrink the object vertically.

Can I achieve this with one or two pinch recognizers, and if so, how?

like image 757
Simon Avatar asked Jan 30 '12 13:01

Simon


1 Answers

You might be able to use a UIPinchGestureRecognizer for this, but you will have to write code to figure out if the pinch is horizontal or vertical. I haven't tested this:

typedef enum {
    PinchAxisNone,
    PinchAxisHorizontal,
    PinchAxisVertical
} PinchAxis;

PinchAxis pinchGestureRecognizerAxis(UIPinchGestureRecognizer *r) {
    UIView *view = r.view;
    CGPoint touch0 = [r locationOfTouch:0 inView:view];
    CGPoint touch1 = [r locationOfTouch:1 inView:view];
    CGFloat tangent = fabsf((touch1.y - touch0.y) / (touch1.x - touch0.x));
    return
        tangent <= 0.2679491924f ? PinchAxisHorizontal // 15 degrees
        : tangent >= 3.7320508076f ? PinchAxisVertical   // 75 degrees
        : PinchAxisNone;
}
like image 120
rob mayoff Avatar answered Oct 30 '22 16:10

rob mayoff