Http://www.techotopia.com/index.php/An_iPhone_iOS_4_Gesture_Recognition_Tutorial_%28Xcode_4%29
In the previous chapter, we have introduced the concept of iPhone Gesture Recognition technology. This chapter aims to demonstrate the usage of various uigesturerecognizer sub-classes through an example. The programs created in this chapter will create and set different gesture identifiers, and then use tags to display the specific information of each detected gesture.
Create a project creating the Gesture Recognition Project
Open xcode and create a view-baseapplication project named recognizer.
Set egress for label
There is only one label component in the view object to display the detected user gesture type. The text on the label will be changed by the Code during the program running, so we need to connect it to an exit. In the project navigation panel of xcode, select the recognizerviewcontroller. h file and change it:
#import <UIKit/UIKit.h>
@interface recognizerViewController : UIViewController {
UILabel *statusLabel;
}
@property (retain, nonatomic) IBOutlet UILabel *statusLabel;
@end
Edit recognizerviewcontroller. M. Add the corresponding exit in the @ synthesize statement and release it in the appropriate place.
#import "recognizerViewController.h"
@implementation recognizerViewController
@synthesize statusLabel;
. .
- (void)dealloc {
[statusLabel release];
[super dealloc];
}
. .
- (void)viewDidUnload {
[super viewDidUnload];
self.statusLabel = nil;
}
. .
@end
Design Interface
Select recognizerviewcontroller. XIB and edit it on the IB panel. Xcode creates a uiview when creating a project. We need to add a label to this uiview. From objectlibrary (View-> utilities-> Object LibraryDrag a label object, and modify the label attribute to align the text center:
Image
CTRL + left (or right-click) drag a line from file 'sowner to the label object, and then release the button. In the pop-up menu, select statuslabel exit.
Set the guseturerecognizers object
We need to use gesturerecognizers in the code to identify click, sweep, rotate, and pinch. Because these recognizers need to be connected to the view object, the ideal method to create them is the viewdidload method of the recognizerviewcontroller class:
- (void)viewDidLoad {
UITapGestureRecognizer *doubleTap =
[[UITapGestureRecognizer alloc]
initWithTarget:self
action:@selector(tapDetected:)];
doubleTap.numberOfTapsRequired = 2;
[self.view addGestureRecognizer:doubleTap];
[doubleTap release];
UIPinchGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc]
initWithTarget:self
action:@selector(pinchDetected:)];
[self.view addGestureRecognizer:pinchRecognizer];
[pinchRecognizer release];
UIRotationGestureRecognizer *rotationRecognizer = [[UIRotationGestureRecognizer alloc]
initWithTarget:self
action:@selector(rotationDetected:)];
[self.view addGestureRecognizer:rotationRecognizer];
[rotationRecognizer release];
UISwipeGestureRecognizer *swipeRecognizer = [[UISwipeGestureRecognizer alloc]
initWithTarget:self
action:@selector(swipeDetected:)];
swipeRecognizer.direction = UISwipeGestureRecognizerDirectionRight;
[self.view addGestureRecognizer:swipeRecognizer];
[swipeRecognizer release];
UILongPressGestureRecognizer *longPressRecognizer = [[UILongPressGestureRecognizer alloc]
initWithTarget:self
action:@selector(longPressDetected:)];
longPressRecognizer.minimumPressDuration = 3;
longPressRecognizer.numberOfTouchesRequired = 1;
[self.view addGestureRecognizer:longPressRecognizer];
[longPressRecognizer release];
[super viewDidLoad];
}
Add Handling Method
After gesturerecognizer is set, you should write a processing method. The processing method will be called by recognizer after the corresponding gesture is detected. These methods are also placed in the recognizerviewcontroller. M file, and these methods refresh the text displayed on the label based on the specific information of the corresponding gesture.
-(IBAction)longPressDetected:(UIGestureRecognizer *)sender{
statusLabel.text = @"Long Press";
}
- (IBAction)swipeDetected:(UIGestureRecognizer *)sender {
statusLabel.text = @"Right Swipe";
}
- (IBAction)tapDetected:(UIGestureRecognizer *)sender {
statusLabel.text = @"Double Tap";
}
- (IBAction)pinchDetected:(UIGestureRecognizer *)sender {
CGFloat scale = [(UIPinchGestureRecognizer *)sender scale];
CGFloat velocity = [(UIPinchGestureRecognizer *)sender velocity];
NSString *resultString = [[NSString alloc] initWithFormat:
@"Pinch - scale = %f, velocity = %f",
scale, velocity];
statusLabel.text = resultString;
[resultString release];
}
- (IBAction)rotationDetected:(UIGestureRecognizer *)sender {
CGFloat radians = [(UIRotationGestureRecognizer *)sender rotation];
CGFloat velocity = [(UIRotationGestureRecognizer *)sender velocity];
NSString *resultString = [[NSString alloc] initWithFormat:
@"Rotation - Radians = %f, velocity = %f",
radians, velocity];
statusLabel.text = resultString;
[resultString release];
}
Test procedure
Finally, compile and run the program. To fully test pinch and rotate gestures, it is best to run programs on physical devices (because the simulator cannot simulate multi-touch ). Connect to the activated debugging device (refer to testingios 4 apps on the iPhone-developer certificates and provisioning profiles), and then click the run button of xcode. After the program runs, perform a gesture and observe the text changes in the label.