http://www.techotopia.com/index.php/An_iPhone_iOS_4_Gesture_Recognition_Tutorial_%28Xcode_4%29
在上一章,我们已经概要介绍了iPhone手势识别技的概念,本章的目的是通过一个实例演示各种UIGestureRecognizer子类的用法。本章所创建的程序,将创建和设置各种不同的手势识别器,然后用标签把每个侦测到的手势的具体信息显示出来。
打开Xcode,创建一个View-baseapplication项目,命名为recognizer。
在View对象中,仅有一个label组件,用于显示侦测到的用户手势类型。label上的文字在程序运行过程中会被代码所改变,因此我们需要为它连接到一个出口上。在xcode的项目导航面板中,选中recognizerViewController.h文件,将它修改为:
#import <UIKit/UIKit.h>
@interface recognizerViewController : UIViewController {
UILabel *statusLabel;
}
@property (retain, nonatomic) IBOutlet UILabel *statusLabel;
@end
接着,编辑recognizerViewController.m。在@synthesize语句中增加相应出口并在合适的地方释放它。
#import "recognizerViewController.h"
@implementation recognizerViewController
@synthesize statusLabel;
. .
- (void)dealloc {
[statusLabel release];
[super dealloc];
}
. .
- (void)viewDidUnload {
[super viewDidUnload];
self.statusLabel = nil;
}
. .
@end
选择recognizerViewController.xib,在IB面板中编辑它。Xcode在创建项目时为我们创建了一个UIView,我们需要在这个UIView中增加一个label。从ObjectLibrary (View-> Utilities -> Object Library) 中拖一个Label对象,然后修改label属性让文本居中对齐:
图片
Ctrl+左键(或者右键)从File’sOwner 拖一条线到Label对象,然后释放按键。在弹出的菜单中选择statusLabel出口。
我们需要在代码中使用gesturerecognizers来识别轻击、轻扫、旋转和捏合。由于这些recognizers需要连接到view对象,因此创建它们的理想地是recognizerViewController类的viewDidLoad方法:
- (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];
}
设置完gesturerecognizer之后,就应当编写处理方法,处理方法将在相应手势侦测到之后被recognizer所调用。这些方法也放在了recognizerViewController.m文件里,同时这些方法会根据相应手势的具体信息来刷新label上显示的文字。
-(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];
}
最后,编译和运行程序。为了能够充分测试捏合和旋转手势,最好是在物理设备中运行程序(因为模拟器上无法模拟多点触摸)。连接已激活的调试设备(参考TestingiOS 4 Apps on the iPhone – Developer Certificates and Provisioning Profiles), 然后点击Xcode的Run按钮。当程序运行后,进行手势动作并观察label中显示文本的变化。