您的位置:首页 > 移动开发 > IOS开发

iOS 4 Gesture Recognizers 应用实战

2014-01-18 19:34 246 查看
原文:http://blog.csdn.net/kmyhy/article/details/7614214

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

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


创建项目CreatingtheGestureRecognitionProject

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


为Label设置出口

在View对象中,仅有一个label组件,用于显示侦测到的用户手势类型。label上的文字在程序运行过程中会被代码所改变,因此我们需要为它连接到一个出口上。在xcode的项目导航面板中,选中recognizerViewController.h文件,将它修改为:

#import<UIKit/UIKit.h>

@interfacerecognizerViewController:UIViewController{

UILabel*statusLabel;

}

@property(retain,nonatomic)IBOutletUILabel*statusLabel;

@end


接着,编辑recognizerViewController.m。在@synthesize语句中增加相应出口并在合适的地方释放它。

#import"recognizerViewController.h"

@implementationrecognizerViewController

@synthesizestatusLabel;

..

-(void)dealloc{

[statusLabelrelease];

[superdealloc];

}

..

-(void)viewDidUnload{

[superviewDidUnload];

self.statusLabel=nil;

}

..

@end




设计界面

选择recognizerViewController.xib,在IB面板中编辑它。Xcode在创建项目时为我们创建了一个UIView,我们需要在这个UIView中增加一个label。从ObjectLibrary(View->Utilities->ObjectLibrary)中拖一个Label对象,然后修改label属性让文本居中对齐:

图片

Ctrl+左键(或者右键)从File’sOwner拖一条线到Label对象,然后释放按键。在弹出的菜单中选择statusLabel出口。


设置GusetureRecognizers对象

我们需要在代码中使用gesturerecognizers来识别轻击、轻扫、旋转和捏合。由于这些recognizers需要连接到view对象,因此创建它们的理想地是recognizerViewController类的viewDidLoad方法:

-(void)viewDidLoad{

UITapGestureRecognizer*doubleTap=

[[UITapGestureRecognizeralloc]

initWithTarget:self

action:@selector(tapDetected:)];

doubleTap.numberOfTapsRequired=2;

[self.viewaddGestureRecognizer:doubleTap];

[doubleTaprelease];

UIPinchGestureRecognizer*pinchRecognizer=[[UIPinchGestureRecognizeralloc]

initWithTarget:self

action:@selector(pinchDetected:)];

[self.viewaddGestureRecognizer:pinchRecognizer];

[pinchRecognizerrelease];

UIRotationGestureRecognizer*rotationRecognizer=[[UIRotationGestureRecognizeralloc]

initWithTarget:self

action:@selector(rotationDetected:)];

[self.viewaddGestureRecognizer:rotationRecognizer];

[rotationRecognizerrelease];

UISwipeGestureRecognizer*swipeRecognizer=[[UISwipeGestureRecognizeralloc]

initWithTarget:self

action:@selector(swipeDetected:)];

swipeRecognizer.direction=UISwipeGestureRecognizerDirectionRight;

[self.viewaddGestureRecognizer:swipeRecognizer];

[swipeRecognizerrelease];

UILongPressGestureRecognizer*longPressRecognizer=[[UILongPressGestureRecognizeralloc]

initWithTarget:self

action:@selector(longPressDetected:)];

longPressRecognizer.minimumPressDuration=3;

longPressRecognizer.numberOfTouchesRequired=1;

[self.viewaddGestureRecognizer:longPressRecognizer];

[longPressRecognizerrelease];

[superviewDidLoad];

}




添加处理方法

设置完gesturerecognizer之后,就应当编写处理方法,处理方法将在相应手势侦测到之后被recognizer所调用。这些方法也放在了recognizerViewController.m文件里,同时这些方法会根据相应手势的具体信息来刷新label上显示的文字。

-(IBAction)longPressDetected:(UIGestureRecognizer*)sender{

statusLabel.text=@"LongPress";

}

-(IBAction)swipeDetected:(UIGestureRecognizer*)sender{

statusLabel.text=@"RightSwipe";

}

-(IBAction)tapDetected:(UIGestureRecognizer*)sender{

statusLabel.text=@"DoubleTap";

}

-(IBAction)pinchDetected:(UIGestureRecognizer*)sender{

CGFloatscale=[(UIPinchGestureRecognizer*)senderscale];

CGFloatvelocity=[(UIPinchGestureRecognizer*)sendervelocity];

NSString*resultString=[[NSStringalloc]initWithFormat:

@"Pinch-scale=%f,velocity=%f",

scale,velocity];

statusLabel.text=resultString;

[resultStringrelease];

}

-(IBAction)rotationDetected:(UIGestureRecognizer*)sender{

CGFloatradians=[(UIRotationGestureRecognizer*)senderrotation];

CGFloatvelocity=[(UIRotationGestureRecognizer*)sendervelocity];

NSString*resultString=[[NSStringalloc]initWithFormat:

@"Rotation-Radians=%f,velocity=%f",

radians,velocity];

statusLabel.text=resultString;

[resultStringrelease];

}




测试程序

最后,编译和运行程序。为了能够充分测试捏合和旋转手势,最好是在物理设备中运行程序(因为模拟器上无法模拟多点触摸)。连接已激活的调试设备(参考TestingiOS
4AppsontheiPhone–DeveloperCertificatesandProvisioningProfiles),然后点击Xcode的Run按钮。当程序运行后,进行手势动作并观察label中显示文本的变化。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: