Tuesday, June 4, 2013

UIScrollView pinchGestureRecognizer example in Objective C (iOS).


UIScrollView pinchGestureRecognizer

The underlying gesture recognizer for pinch gestures. (read-only)

@property(nonatomic, readonly) UIPinchGestureRecognizer *pinchGestureRecognizer

Discussion of [UIScrollView pinchGestureRecognizer]
Your application accesses this property when it wants to more precisely control which pinch gestures are recognized by the scroll view.

UIScrollView pinchGestureRecognizer example.
You have access to the pinchGestureRecognizer of the UIScrollView. Then you can ask for the center coordinate of the pinch gesture with

- (CGPoint)locationInView:(UIView *)view
So to get the midpoint between two fingers in your scrollView you can write :

CGPoint midpoint = [scrollView.pinchGestureRecognizer locationInView:scrollView];

Example of [UIScrollView pinchGestureRecognizer].
UIPinchGestureRecognizer *pinchRecognizer = self.pinchGestureRecognizer;
pinchRecognizer.enabled = NO;
or

UIPanGestureRecognizer *panRecognizer = self.scrollView.panGestureRecognizer;
panRecognizer.enabled = NO;

UIScrollView pinchGestureRecognizer example.
@implementation CustomView

- (void)overridePinchForScroll:(UIScrollView *)scroll
{
    [self addGestureRecognizer:scroll.panGestureRecognizer];
    [self addGestureRecognizer:scroll.pinchGestureRecognizer];
}

@end

End of UIScrollView pinchGestureRecognizer example article.