How To Define Custom Gestures In iPhone?

Defining a custom gesture is tricky. The tricky part is being flexible when defining what constitutes a gesture . In our sample, we’re going to define a gesture shaped like a check mark.

Step 1: create a new project in Xcode using the view-based application template. We are going to need a function from CGPointUtils, so add CGPointUtils.h, and CGPointUtils.c to this project’s classes folder.

Step 2: You can see that we’ve defined a minimum angle of 50 degress and a maximum angle of 135 degress in our 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: Let’s take a look at the touch methods, which we are implimented in controller class. First in touchesBegan:withEvent:, we determine the point that the user is currently touching and store that value in lastPreviousPoint and lastCurrentPoint. Another one is touchesMoved:withEvent:, we calculate the angle between the line from the current touch’s previous position to its current position, and the line between the two points stored in the lastPreviousPoint and lastCurrentPoint instance variables.




Step 5: Compile and run , and try out the gesture. (See figure 1)

Figure 1: Our checkmark gesture application.

Leave a Comment:

1 comment
Naren says December 24, 2010

where is function distanceBetweenPoints, angleBetweenLines. Please try to post the whole code

Add Your Reply