[BS-25] IOS中手势UIGestureRecognizer概述
IOS中手势UIGestureRecognizer概述
一、概述
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,能轻松识别用户在某个view上面做的一些常见手势。UIGestureRecognizer是一个抽象的基类,定义了所有手势的基本行为,不能直接使用。需使用UIGestureRecognizer的子类才能处理具体的手势,具体如下:
UITapGestureRecognizer //(敲击)
UIPinchGestureRecognizer //(捏合,用于缩放)
UIPanGestureRecognizer //(拖拽)
UISwipeGestureRecognizer //(轻扫)
UIRotationGestureRecognizer //(旋转)
UILongPressGestureRecognizer //(长按)
从名字上我们就能知道,
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* doubleRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:selfaction:@selector(DoubleTap:)]; doubleRecognizer.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)
摇晃手势,将出现撤销与重做菜单。主要是针对用户文本输入的。
四、手势识别的状态
typedef NS_ENUM(NSInteger, UIGestureRecognizerState) {
// 没有触摸事件发生,所有手势识别的默认状态
UIGestureRecognizerStatePossible,
// 一个手势已经开始但尚未改变或者完成时
UIGestureRecognizerStateBegan,
// 手势状态改变
UIGestureRecognizerStateChanged,
// 手势完成
UIGestureRecognizerStateEnded,
// 手势取消,恢复至Possible状态
UIGestureRecognizerStateCancelled,
// 手势失败,恢复至Possible状态
UIGestureRecognizerStateFailed,
// 识别到手势识别
UIGestureRecognizerStateRecognized = UIGestureRecognizerStateEnded
};
五、手势应用实例
每一个手势识别器的用法都差不多,比如UITapGestureRecognizer的使用步骤如下; //创建手势识别器对象 UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init]; //设置手势识别器对象的具体属性 tap.numberOfTapsRequired = 2; // 连续敲击2次 tap.numberOfTouchesRequired = 2; // 需要2根手指一起敲击 //添加手势识别器到对应的view上 [self.iconView addGestureRecognizer:tap]; //监听手势的触发 [tap addTarget:self action:@selector(tapIconView:)]; //eg1.敲击实例 @interface GRViewController () @property (weak, nonatomic) IBOutlet UIImageView *iconView; @end @implementation GRViewController - (void)viewDidLoad { [super viewDidLoad]; [self testTap2]; } - (void)testTap2 { UIGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapView)]; tap.delegate = self; [self.iconView addGestureRecognizer:tap]; } #pragma mark - 代理方法 /** * 当点击view的时候,会先调用这个方法 */ - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch { CGPoint pos = [touch locationInView:touch.view]; if (pos.x <= self.iconView.frame.size.width * 0.5) { return YES; } return NO; } - (void)testTap { // 1.创建手势识别器对象 UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init]; // 连续敲击2次,手势才能识别成功 tap.numberOfTapsRequired = 2; tap.numberOfTouchesRequired = 2; // 2.添加手势识别器对象到对应的view [self.iconView addGestureRecognizer:tap]; // 3.添加监听方法(识别到了对应的手势,就会调用监听方法) [tap addTarget:self action:@selector(tapView)]; } - (void)tapView { NSLog(@"-----我敲击了屏幕!!"); } @end //eg2.长按和轻扫实例 @interface GRViewController () @property (weak, nonatomic) IBOutlet UIView *redView; @end @implementation GRViewController - (void)viewDidLoad { [super viewDidLoad]; UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeView)]; swipe.direction = UISwipeGestureRecognizerDirectionUp; [self.redView addGestureRecognizer:swipe]; } - (void)swipeView { NSLog(@"swipeView"); } - (void)testLongPress { UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] init]; [longPress addTarget:self action:@selector(longPressView)]; // 至少长按2秒 longPress.minimumPressDuration = 2; // 在触发手势之前,50px范围内长按有效 longPress.allowableMovement = 50; [self.redView addGestureRecognizer:longPress]; } - (void)longPressView { NSLog(@"长按了红色的view"); } @end //eg3. 缩放和旋转实例 @interface GRViewController () @property (weak, nonatomic) IBOutlet UIImageView *iconView; @end @implementation GRViewController - (void)viewDidLoad { [super viewDidLoad]; [self testPinchAndRotate]; } #pragma mark - 手势识别器的代理方法 /** * 是否允许多个手势识别器同时有效 * Simultaneously : 同时地 */ - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer { return YES; } #pragma mark - 缩放 + 旋转 - (void)testPinchAndRotate { [self testPinch]; [self testRotate]; } #pragma mark - 缩放手势(捏合手势) - (void)testPinch { UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchView:)]; pinch.delegate = self; [self.iconView addGestureRecognizer:pinch]; } - (void)pinchView:(UIPinchGestureRecognizer *)pinch { pinch.view.transform = CGAffineTransformScale(pinch.view.transform, pinch.scale, pinch.scale); pinch.scale = 1; // 这个真的很重要!!! } #pragma mark - 旋转手势 - (void)testRotate { UIRotationGestureRecognizer *recognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotateView:)]; recognizer.delegate = self; [self.iconView addGestureRecognizer:recognizer]; } - (void)rotateView:(UIRotationGestureRecognizer *)recognizer { recognizer.view.transform = CGAffineTransformRotate(recognizer.view.transform, recognizer.rotation); recognizer.rotation = 0; // 这个很重要!!!!! } @end //eg4. 拖拽实例 @interface GRViewController () @property (weak, nonatomic) IBOutlet UIView *purpleView; @end @implementation GRViewController - (void)viewDidLoad { [super viewDidLoad]; UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panView:)]; [self.purpleView addGestureRecognizer:pan]; } - (void)panView:(UIPanGestureRecognizer *)pan { switch (pan.state) { case UIGestureRecognizerStateBegan: // 开始触发手势 break; case UIGestureRecognizerStateEnded: // 手势结束 break; default: break; } // 1.在view上面挪动的距离 CGPoint translation = [pan translationInView:pan.view]; CGPoint center = pan.view.center; center.x += translation.x; center.y += translation.y; pan.view.center = center; // 2.清空移动的距离 [pan setTranslation:CGPointZero inView:pan.view]; } @end
参考:http://blog.csdn.net/likendsl/article/details/7554150