Detecting Pinch is pretty easy. First, when the gesture begins, we check to make sure there are two touches,because pinches are two-finger gestures. If there are two , we store the distance between them. Then as the gesture progresses, we keep checking the distance between the user’s fingers, and if the distance increases or discreases more than a certain amount , we know there’s been a pinch.

Step 1: Create a new project in Xcode, using the view based application template. There are two file CGPointUtils.c and CGPointUtils.h. Drag both files to the classes folder of your project.

Step 2: We define the method for erasing the label,and also define a constant that identifies the minimum change in distance between the fingers that constitutes a pinch in the application header file.

#import <UIKit/UIKit.h>

#define kMinimumPinchDelta 100

@interface PinchMeViewController : UIViewController {
       
        IBOutlet UILabel*label;
        CGFloat  initialDistance;

}

@property(nonatomic,retain) UILabel*label;
@property CGFloat  initialDistance;

-(void)eraseLabel;

Step 3:  Double click your .xib file, open it to Interface Builder. Make sure the view is set to accept multiple touches, and drag a single label over it to. Double click the label and delete the text. Next, control drag from the File’s Owner icon to the label, and connect it to the label outlet. Save and close the nib, and go back to Xcode.

Step 4: In the controller classes we have to mention some methods. The touchesBegan:withEvent: method, we check to see if this touch involves two fingers. Another one is touchesMoved:withEvent:, we again check to see if we have two touches, and if we do we calculate the distance between the two touches:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
        if([touches count] == 2){
                NSArray *twoTouches = [touches allObjects];
                UITouch *first = [twoTouches objectAtIndex:0];
                UITouch *second = [twoTouches objectAtIndex:1];
                initialDistance = distanceBetweenPoints(
                [first locationInView: self.view],
                [second locationInView:self.view]);
        }
}

       
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
                if([touches count] == 2)
                {
                 NSArray *twoTouches = [touches allObjects];
                 UITouch *first = [twoTouches objectAtIndex:0];
                 UITouch *second = [twoTouches objectAtIndex:1];
                 CGFloat currentDistance = distanceBetweenPoints(
                [first locationInView: self.view],
                [second locationInView:self.view]);
                 
                if(initialDistance == 0)
                 initialDistance = currentDistance;
                else if (currentDistance - initialDistance >kMinimumPinchDelta){
                 label.text = @"Outward Pinch";
                 [self performSelector:@selector(eraselabel)
                           withObject:nil
                           afterDelay:1.6f];
        }
                               
        else if(initialDistance - currentDistance >kMinimumPinchDelta){
         label.text = @"Inward Pinch";
         [self performSelector:@selector(eraselabel)
               withObject:nil
                   afterDelay:1.6f];                   

                }
         }      
 }

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
        initialDistance = 0;
                       
       
}

Step 5: Now compile and run to give it a try, if you are on the simulator, remember that you can simulate a pinch by holding down the option key and clicking and dragging in the simulator window using your mouse.

You can Download SourceCode from here PinchMe

Did you enjoy this article?
Share
the
Love
Get Free Updates