This post deals with detecting multi-touches on iPhone screen.
One of the most important feature of iPhone is its touchscreen which can detect multiple touches. This feature allows most natural interaction with objects visible on iPhone screen.
Lets start
1) Create a new view based application and name it Multi-Touch.
2)Drag and drop an image into resource folder. Lets say the name of image is apple.jpeg.
3) Open MultiTouchViewController.xib file
4) Populate the view with ImageView. Ensure that ImageView covers the entire view.
5) Select image property on the ImageView to apple.jpeg in its Attribute window.
6) Open MultiTouchViewController.h and write the following highlighted code
7) Drag the File’s Owner item to the ImageView view. Select imageView.
Write the following code in MultiTouchViewController.m
@synthesize imageView;
//---fired when the user finger(s) touches the screen---
-(void) touchesBegan: (NSSet *) touches withEvent: (UIEvent *) event {
//---get all touches on the screen---
NSSet *allTouches = [event allTouches];
//---compare the number of touches on the screen
switch ([allTouches count]) {
//---single touch---
case 1: {
//---get info of the touch---
UITouch *touch = [[allTouches allObjects] objectAtIndex:0];
//---compare the touches---
switch ([touch tapCount]) {
//---single tap---
case 1: {
imageView.contentMode = UIViewContentModeScaleAspectFit;
}
break;
//---double tap---
case 2: {
imageView.contentMode = UIViewContentModeCenter;
}
break;
}
}
break;
}
}
As we see above that we use “touchesBegan:” delegation method which is fired when user touches the screen. NSSet object allTouches tells that how many fingers are touching the screen. We extract details of the first touch by using the allObjects method of the NSSet object to return an NSArray object. The UITouch object (touch) contains the tapCount property, which tells us whether the user has single-tapped the screen or performed a double tap (or more).
9) Save and Build the application.
10) Single tap the image to enlarge it and double tap to return to its original size.


