一、概述
iPhone中处理触摸屏的操作,在3.2之前是主要使用的是由UIResponder而来的如下4种方式:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
但是这种方式甄别不同的手势操作实在是麻烦,需要你自己计算做不同的手势分辨。后来。。。
苹果就给出了一个比较简便的方式,就是使用UIGestureRecognizer。
二、UIGestureRecognizer
UIGestureRecognizer基类
是一个抽象类,我们主要是使用它的子类(名字包含链接,可以点击跳到ios Developer library,看官方文档):
从名字上我们就能知道,
Tap(点击)、Pinch(捏合)、Rotation(旋转)、Swipe(滑动,快速移动,是用于监测滑动的方向的)、Pan (拖移,慢速移动,是用于监测偏移的量的)以及 LongPress(长按)。
举个例子,可以在viewDidLoad函数里面添加:
- -(void) viewDidLoad
- {
- [super viewDidLoad];
- // Do any additional setup after loading the view from its nib.
- UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanFrom:)];
- [self.view addGestureRecognizer:panRecognizer];//关键语句,给self.view添加一个手势监测;
- panRecognizer.maximumNumberOfTouches = 1;
- panRecognizer.delegate = self;
- [panRecognizer release];
- }
其核心就是设置delegate和在需要手势监测的view上使用addGestureRecognizer添加指定的手势监测。
当然要记得在作为delegate的view的头文件加上<UIGestureRecognizerDelegate>。
不过有些手势是关联的,怎么办呢?例如 Tap 与 LongPress、Swipe与 Pan,或是 Tap 一次与Tap 兩次。
手势识别是具有互斥的原则的,比如单击和双击,如果它识别出一种手势,其后的手势将不被识别。所以对于关联手势,要做特殊处理以帮助程序甄别,应该把当前手势归结到哪一类手势里面。
比如,单击和双击并存时,如果不做处理,它就只能发送出单击的消息。为了能够识别出双击手势,就需要做一个特殊处理逻辑,即先判断手势是否是双击,在双击失效的情况下作为单击手势处理。使用
[A requireGestureRecognizerToFail:B]函数,它可以指定当A手势发生时,即便A已经滿足条件了,也不会立刻触发,会等到指定的手势B确定失败之后才触发。
- - (void)viewDidLoad
- {
- // 单击的 Recognizer
- UITapGestureRecognizer* singleRecognizer;
- singleRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:selfaction:@selector(SingleTap:)];
- //点击的次数
- singleTapRecognizer.numberOfTapsRequired = 1; // 单击
- //给self.view添加一个手势监测;
- [self.view addGestureRecognizer:singleRecognizer];
- // 双击的 Recognizer
- UITapGestureRecognizer* double;
- doubleRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:selfaction:@selector(DoubleTap:)];
- doubleTapRecognizer.numberOfTapsRequired = 2; // 双击
- //关键语句,给self.view添加一个手势监测;
- [self.view addGestureRecognizer:doubleRecognizer];
- // 关键在这一行,双击手势确定监测失败才会触发单击手势的相应操作
- [singleRecognizer requireGestureRecognizerToFail:doubleRecognizer];
- [singleRecognizer release];
- [doubleRecognizer release];
- }
- -(void)SingleTap:(UITapGestureRecognizer*)recognizer
- {
- //处理单击操作
- }
- -(void)DoubleTap:(UITapGestureRecognizer*)recognizer
- {
- //处理双击操作
- }
三、iphone操作手势的大概种类
1.点击(Tap)
点击作为最常用手势,用于按下或选择一个控件或条目(类似于普通的鼠标点击)、
2.拖动(Drag)
拖动用于实现一些页面的滚动,以及对控件的移动功能。
3.滑动(Flick)
滑动用于实现页面的快速滚动和翻页的功能。
4.横扫(Swipe)
横扫手势用于激活列表项的快捷操作菜单
5.双击(Double Tap)
双击放大并居中显示图片,或恢复原大小(如果当前已经放大)。同时,双击能够激活针对文字编辑菜单。
6.放大(Pinch open)
放大手势可以实现以下功能:打开订阅源,打开文章的详情。在照片查看的时候,放大手势也可实现放大图片的功能。
7.缩小(Pinch close)
缩小手势,可以实现与放大手势相反且对应的功能的功能:关闭订阅源退出到首页,关闭文章退出至索引页。在照片查看的时候,缩小手势也可实现缩小图片的功能。
8.长按(Touch &Hold)
在我的订阅页,长按订阅源将自动进入编辑模式,同时选中手指当前按下的订阅源。这时可直接拖动订阅源移动位置。
针对文字长按,将出现放大镜辅助功能。松开后,则出现编辑菜单。
针对图片长按,将出现编辑菜单。
9.摇晃(Shake)
摇晃手势,将出现撤销与重做菜单。主要是针对用户文本输入的。
http://blog.csdn.net/likendsl/article/details/7554150
iOS-响应上下左右滑动手势
-(void)viewDidLoad{
UISwipeGestureRecognizer *recognizer;
recognizer = [[UISwipeGestureRecognizeralloc]initWithTarget:selfaction:@selector(handleSwipeFrom:)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionRight)];
[[selfview] addGestureRecognizer:recognizer];
[recognizerrelease];
recognizer = [[UISwipeGestureRecognizeralloc]initWithTarget:selfaction:@selector(handleSwipeFrom:)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionLeft)];
[[selfview] addGestureRecognizer:recognizer];
[recognizerrelease];
recognizer = [[UISwipeGestureRecognizeralloc]initWithTarget:selfaction:@selector(handleSwipeFrom:)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionUp)];
[[selfview] addGestureRecognizer:recognizer];
[recognizerrelease];
UISwipeGestureRecognizer *recognizer;
recognizer = [[UISwipeGestureRecognizeralloc]initWithTarget:selfaction:@selector(handleSwipeFrom:)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionDown)];
[[selfview] addGestureRecognizer:recognizer];
[recognizerrelease];
}
-(void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer{
if(recognizer.direction==UISwipeGestureRecognizerDirectionDown) {
NSLog(@"swipe down");
//执行程序
}
if(recognizer.direction==UISwipeGestureRecognizerDirectionUp) {
NSLog(@"swipe up");
//执行程序
}
if(recognizer.direction==UISwipeGestureRecognizerDirectionLeft) {
NSLog(@"swipe left");
//执行程序
}
if(recognizer.direction==UISwipeGestureRecognizerDirectionRight) {
NSLog(@"swipe right");
//执行程序
}
}
http://blog.csdn.net/shijiucdy/article/details/7364143
iphone练习之手势识别(双击、捏、旋转、拖动、划动、长按)
首先新建一个基于Sigle view Application的项目,名为GestureTest;我的项目结构如下:
往viewController.xib文件里拖动一个imageView,并使覆盖整个屏幕,改动属性为:
viewController.h文件:
- #import <UIKit/UIKit.h>
- @interface ViewController : UIViewController{
- IBOutlet UIImageView *imageView;
- }
- @property (nonatomic,retain)IBOutlet UIImageView *imageView;
- @end
并使xib文件里的imageView与之连接;
然后是viewController.m文件的实现部分:
- @synthesize imageView;
- CGFloat lastScaleFactor=1;//放大、缩小
- CGFloat netRotation;//旋转
- CGPoint netTranslation;//平衡
- NSArray *images;//图片数组
- int imageIndex=0;//数组下标
- - (void)viewDidLoad
- {
- //1、创建手势实例,并连接方法handleTapGesture,点击手势
- UITapGestureRecognizer *tapGesture=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleTapGesture:)];
- //设置手势点击数,双击:点2下
- tapGesture.numberOfTapsRequired=2;
- // imageView添加手势识别
- [imageView addGestureRecognizer:tapGesture];
- //释放内存
- [tapGesture release];
- //2、手势为捏的姿势:按住option按钮配合鼠标来做这个动作在虚拟器上
- UIPinchGestureRecognizer *pinchGesture=[[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(handlePinchGesture:)];
- [imageView addGestureRecognizer:pinchGesture];//imageView添加手势识别
- [pinchGesture release];
- //3、旋转手势:按住option按钮配合鼠标来做这个动作在虚拟器上
- UIRotationGestureRecognizer *rotateGesture=[[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(handleRotateGesture:)];
- [imageView addGestureRecognizer:rotateGesture];
- [rotateGesture release];
- //4、拖手势
- UIPanGestureRecognizer *panGesture=[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(handlePanGesture:)];
- // [imageView addGestureRecognizer:panGesture];
- [panGesture release];
- //5、划动手势
- images=[[NSArray alloc]initWithObjects:@"cell.jpg",@"heihua.jpg",@"xuanyi.jpg", nil];
- //右划
- UISwipeGestureRecognizer *swipeGesture=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipeGesture:)];
- [imageView addGestureRecognizer:swipeGesture];
- [swipeGesture release];
- //左划
- UISwipeGestureRecognizer *swipeLeftGesture=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipeGesture:)];
- swipeGesture.direction=UISwipeGestureRecognizerDirectionLeft;//不设置黑夜是右
- [imageView addGestureRecognizer:swipeLeftGesture];
- [swipeLeftGesture release];
- //6、长按手势
- UILongPressGestureRecognizer *longpressGesutre=[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(handleLongpressGesture:)];
- //长按时间为1秒
- longpressGesutre.minimumPressDuration=1;
- //允许15秒中运动
- longpressGesutre.allowableMovement=15;
- //所需触摸1次
- longpressGesutre.numberOfTouchesRequired=1;
- [imageView addGestureRecognizer:longpressGesutre];
- [longpressGesutre release];
- [super viewDidLoad];
- // Do any additional setup after loading the view, typically from a nib.
- }
- //双击屏幕时会调用此方法,放大和缩小图片
- -(IBAction)handleTapGesture:(UIGestureRecognizer*)sender{
- //判断imageView的内容模式是否是UIViewContentModeScaleAspectFit,该模式是原比例,按照图片原时比例显示大小
- if(sender.view.contentMode==UIViewContentModeScaleAspectFit){
- //把imageView模式改成UIViewContentModeCenter,按照图片原先的大小显示中心的一部分在imageView
- sender.view.contentMode=UIViewContentModeCenter;
- }else{
- sender.view.contentMode=UIViewContentModeScaleAspectFit;
- }
- }
- //捏的手势,使图片放大和缩小,捏的动作是一个连续的动作
- -(IBAction)handlePinchGesture:(UIGestureRecognizer*)sender{
- //得到sender捏手势的大小
- CGFloat factor=[(UIPinchGestureRecognizer*)sender scale];
- if(factor>1){
- //图片放大
- sender.view.transform=CGAffineTransformMakeScale(lastScaleFactor+(factor-1), (lastScaleFactor+(factor-1)));
- }else{
- //缩小
- sender.view.transform=CGAffineTransformMakeScale(lastScaleFactor*factor, lastScaleFactor*factor);
- }
- //状态是否结束,如果结束保存数据
- if(sender.state==UIGestureRecognizerStateEnded){
- if(factor>1){
- lastScaleFactor+=(factor-1);
- }else{
- lastScaleFactor*=factor;
- }
- }
- }
- //旋转手势
- -(IBAction)handleRotateGesture:(UIGestureRecognizer*)sender{
- //浮点类型,得到sender的旋转度数
- CGFloat rotation=[(UIRotationGestureRecognizer*)sender rotation];
- //旋转角度CGAffineTransformMakeRotation
- CGAffineTransform transform=CGAffineTransformMakeRotation(rotation+netRotation);
- //改变图像角度
- sender.view.transform=transform;
- //状态结束,保存数据
- if(sender.state==UIGestureRecognizerStateEnded){
- netRotation+=rotation;
- }
- }
- //拖手势
- -(IBAction)handlePanGesture:(UIGestureRecognizer*)sender{
- //得到拖的过程中的xy坐标
- CGPoint translation=[(UIPanGestureRecognizer*)sender translationInView:imageView];
- //平移图片CGAffineTransformMakeTranslation
- sender.view.transform=CGAffineTransformMakeTranslation(netTranslation.x+translation.x, netTranslation.y+translation.y);
- //状态结束,保存数据
- if(sender.state==UIGestureRecognizerStateEnded){
- netTranslation.x+=translation.x;
- netTranslation.y+=translation.y;
- }
- }
- //划动手势
- -(IBAction)handleSwipeGesture:(UIGestureRecognizer*)sender{
- //划动的方向
- UISwipeGestureRecognizerDirection direction=[(UISwipeGestureRecognizer*) sender direction];
- //判断是上下左右
- switch (direction) {
- case UISwipeGestureRecognizerDirectionUp:
- NSLog(@"up");
- break;
- case UISwipeGestureRecognizerDirectionDown:
- NSLog(@"down");
- break;
- case UISwipeGestureRecognizerDirectionLeft:
- NSLog(@"left");
- imageIndex++;//下标++
- break;
- case UISwipeGestureRecognizerDirectionRight:
- NSLog(@"right");
- imageIndex--;//下标--
- break;
- default:
- break;
- }
- //得到不越界不<0的下标
- imageIndex=(imageIndex<0)?([images count]-1):imageIndex%[images count];
- //imageView显示图片
- imageView.image=[UIImage imageNamed:[images objectAtIndex:imageIndex]];
- }
- //长按手势
- -(IBAction)handleLongpressGesture:(UIGestureRecognizer*)sender{
- //创建警告
- UIActionSheet *actionSheet=[[UIActionSheet alloc]initWithTitle:@"Image options" delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:@"Save Image",@"Copy", nil];
- //当前view显示警告
- [actionSheet showInView:self.view];
- [actionSheet release];
- }
- -(void)dealloc{
- [images release];
- [imageView release];
- [super dealloc];
- }
嗯,试试效果吧!