iOS 4 Gesture Recognizers 应用实战

本文介绍如何在 iPhone 上使用 iOS 4 开发应用时创建和设置各种手势识别器,包括轻击、轻扫、旋转和捏合等,并通过实例演示了如何在视图中实现这些功能。通过代码实现,可以实时显示用户执行的手势类型,提升用户体验。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://www.techotopia.com/index.php/An_iPhone_iOS_4_Gesture_Recognition_Tutorial_%28Xcode_4%29

在上一章,我们已经概要介绍了iPhone手势识别技的概念,本章的目的是通过一个实例演示各种UIGestureRecognizer子类的用法。本章所创建的程序,将创建和设置各种不同的手势识别器,然后用标签把每个侦测到的手势的具体信息显示出来。

创建项目Creating the Gesture Recognition Project

打开Xcode,创建一个View-baseapplication项目,命名为recognizer。

为Label设置出口

在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出口。

设置GusetureRecognizers对象

我们需要在代码中使用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中显示文本的变化。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值