How to detect Pinch in Iphone?

pinch

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

Comments 3

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

    where can I get the files?

  2. 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.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">