Question

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?

Was it helpful?

Solution

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;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top