How To Detect Pinch In iPhone?

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.




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:




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

Leave a Comment:

Raphael says May 14, 2010

Thanks for your tutorial. I’ve got a problem with the
#include “CGPointUtils.h”
#include “CGPointUtils.c”

where can I get the files?

Sushant says May 14, 2010

You have to included this two files externally. I have uploaded source code “PinchMe”, from there you can get the CGPointUtils.h and CGPointUtils.m files.

Aman Aggarwal says October 6, 2010

After two tries it says uncaught exception and terminates

Add Your Reply