一.手势UIGestureRecognier简介
iOS 3.2之后,苹果推出了手势识别功能(Gesture Recognizer),在触摸事件处理方面,大大简化了开发者的开发难度。利用UIGestureRecognizer,能轻松识别用户在某个view上面做的一些常见手势。UIGestureRecognizer是一个抽象类,对iOS中的事件传递机制面向应用进行封装,将手势消息的传递抽象为了对象。其中定义了所有手势的基本行为,使用它的子类才能处理具体的手势。
二.手势的抽象类——UIGestureRecognizer
UIGestureRecognizer将一些和手势操作相关的方法抽象了出来,但它本身并不实现什么手势,因此,在开发中,我们一般不会直接使用UIGestureRecognizer的对象,而是通过其子类进行实例化,iOS系统给我们提供了许多用于实例的子类,这些我们后面再说,我们先来看一下,UIGestureRecognizer中抽象出了哪些方法。
1.初始化方法
UIGestureRecognizer类为其子类准备好了一个统一的初始化方法,无论什么样的手势动作,其执行的结果都是一样的:触发一个方法,可以使用下面的方法进行统一的初始化:
-(instancetype)initWithTarget:(nullable id)target action:(nullable SEL)action
当然,如果我们使用alloc-init的方式,也是可以的,下面的方法可以为手势添加触发的selector:
-(void)addTarget:(id)target action:(SEL)action;
与之相对应的,我们也可以将一个selector从其手势对象上移除:
-(void)removeTarget:(nullable id)target action:(nullable SEL)action;
因为addTarget方式的存在,iOS系统允许一个手势对象可以添加多个selector触发方法,并且触发的时候,所有添加的selector都会被执行,我们以点击手势示例如下:
-(void)viewDidLoad{
[super viewDidLoad];
UITapGestureRecognizer*tap1=[[UITapGestureRecognizer alloc]initWithTarget:self action: selector(tap1:)];
[tap1 addTarget:self action: selector(tap2:)];
[self.view addGestureRecognizer:tap1];
}
-(void)tap1:(UITapGestureRecognizer*)tap
{
NSLog( "%s",__func__);
}
-(void)tap2:(UITapGestureRecognizer*)tap
{
NSLog( "%s",__func__);
}
点击屏幕,打印内容如下,说明两个方法都触发了
2.手势状态
UIgestureRecognizer类中有如下一个属性,里面枚举了一些手势的当前状态:
property(nonatomic,readonly)UIGestureRecognizerState state;
枚举值如下:
typedef NS_ENUM(NSInteger,UIGestureRecognizerState){
UIGestureRecognizerStatePossible,//默认的状态,这个时候的手势并没有具体的情形状态
UIGestureRecognizerStateBegan,//手势开始被识别的状态
UIGestureRecognizerStateChanged,//手势识别发生改变的状态
UIGestureRecognizerStateEnded,//手势识别结束,将会执行触发的方法
UIGestureRecognizerStateCancelled,//手势识别取消
UIGestureRecognizerStateFailed,//识别失败,方法将不会被调用
UIGestureRecognizerStateRecognized=UIGestureRecognizerStateEnded
};
3.常用属性和方法
//手势代理代理中有一些手势触发的方法,后面拿出来详细说明
property(nullable,nonatomic,weak)id<UIGestureRecognizerDelegate>delegate;
//设置手势是否有效
property(nonatomic,getter=isEnabled)BOOL enabled;
//获取手势所在的View
property(nullable,nonatomic,readonly)UIView*view;
//默认是YES。当识别到手势的时候,终止touchesCancelled:withEvent:或pressesCancelled:withEvent:发送的所有触摸事件。
property(nonatomic)BOOL cancelsTouchesInView;
//默认为NO,在触摸开始的时候,就会发消息给事件传递链,如果设置为YES,在触摸没有被识别失败前,都不会给事件传递链发送消息。
property(nonatomic)BOOL delaysTouchesBegan;
//默认为YES。这个属性设置手势识别结束后,是立刻发送touchesEnded或pressesEnded消息到事件传递链或者等待一个很短的时间后,如果没有接收到新的手势识别任务,再发送。
property(nonatomic)BOOL delaysTouchesEnded;
property(nonatomic,copy)NSArray<NSNumber*>*allowedTouchTypes NS_AVAILABLE_IOS(9_0);//Array of UITouchType's as NSNumbers.
property(nonatomic,copy)NSArray<NSNumber*>*allowedPressTypes NS_AVAILABLE_IOS(9_0);//Array of UIPressTypes as NSNumbers.
//[A requireGestureRecognizerToFail:B]手势互斥它可以指定当A手势发生时,即便A已经滿足条件了,也不会立刻触发,会等到指定的手势B确定失败之后才触发。
-(void)requireGestureRecognizerToFail:(UIGestureRecognizer*)otherGestureRecognizer;
//获取当前触摸的点
-(CGPoint)locationInView:(nullable UIView*)view;
//设置触摸点数
-(NSUInteger)numberOfTouches;
//获取某一个触摸点的触摸位置
-(CGPoint)locationOfTouch:(NSUInteger)touchIndex inView:(nullable UIView*)view;
3.1个别属性详解
其中几个BOOL值的属性,对于手势触发的控制也十分重要,下面我们举个栗子来详细说明一下以下三个方法。
property(nonatomic)BOOL cancelsTouchesInView;
property(nonatomic)BOOL delaysTouchesBegan;
property(nonatomic)BOOL delaysTouchesEnded;
-(void)viewDidLoad{
[super viewDidLoad];
UIPanGestureRecognizer*pan=[[UIPanGestureRecognizer alloc]initWithTarget:self action: selector(pan:)];
pan.cancelsTouchesInView=NO;
//pan.delaysTouchesBegan=YES;
[self.view addGestureRecognizer:pan];
}
-(void)touchesMoved:(NSSet<UITouch*>*)touches withEvent:(UIEvent*)event
{
NSLog( "touchMoved手势触发");
}
-(void)pan:(UIPanGestureRecognizer*)pan{
NSLog( "pan手势触发");
}
pan.cancelsTouchesInView属性默认设置为YES,如果识别到了手势,系统将会发送touchesCancelled:withEvent:消息在其时间传递链上,终止触摸事件的传递,也就是说默认当识别到手势时,touch事件传递的方法将被终止而不执行,如果设置为NO,touch事件传递的方法仍然会被执行,上例中我们使用了拖拽手势和touchesMoved两个触发方式,当我们把cancelTouchesInView设置为NO时,在屏幕上滑动,两种方式都在触发,打印如下:
而当我们将pan.cancelsTouchesInView = YES属性设置为YES时,打印结果如下
我们发现touchesMoved的方法仍然被调用了,这是为什么呢?这就涉及到第二个属性delaysTouchesBegan,这是因为手势识别是有一个过程的,拖拽手势需要一个很小的手指移动的过程才能被识别为拖拽手势,而在一个手势触发之前,是会一并发消息给事件传递链的,所以才会有最开始的几个touchMoved方法被调用,当识别出拖拽手势以后,就会终止touch事件的传递。
delaysTouchesBgan属性用于控制这个消息的传递时机,默认这个属性为NO,此时在触摸开始的时候,就会发消息给事件传递链,如果我们设置为YES,在触摸没有被识别失败前,都不会给事件传递链发送消息。
因此当我们设置pan.delaysTouchesBegan = YES;时打印内容如下
因为此时在拖拽手势识别失败之前,都不会给时间传递链发送消息,所以就不会在调用touchesMoved触发事件了
而delaysTouchesEnded属性默认是YES,当设为YES时在手势识别结束后,会等待一个很短的时间,如果没有接收到新的手势识别任务,才会发送touchesEnded消息到事件传递链,设置为NO之后会立刻发送touchesEnded消息到事件传递链我们同样来看一个例子:
- (void)viewDidLoad {
[super viewDidLoad];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap:)];
tap.numberOfTapsRequired = 3;
// tap.cancelsTouchesInView = NO;
// tap.delaysTouchesBegan = YES;
tap.delaysTouchesEnded = NO;
[self.view addGestureRecognizer:tap];
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
NSLog(@"touchBegan手势开始");
}
-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
NSLog(@"touchEnd手势触发结束");
}
-(void)tap:(UITapGestureRecognizer *)tap
{
NSLog(@"tap手势触发");
}
当tap.delaysTouchesEnded = NO;时,轻拍三下屏幕,打印如下
我们发现我们每点击一下,都会立即发送touchesEnded消息到事件传递链。
而当tap.delaysTouchesEnded = YES;时,轻拍三下屏幕,打印如下
等三下轻拍手势识别结束后,才会发送消息到事件传递链。
3.2重点方法详解-手势间的互斥处理
同一个View上是可以添加多个手势对象的,默认这些手势是互斥的,一个手势触发了就会默认屏蔽其他相似的手势动作。比如,单击和双击并存时,如果不做处理,它就只能发送出单击的消息。为了能够识别出双击手势,就需要用下面的方法一个特殊处理逻辑,即先判断手势是否是双击,在双击失效的情况下作为单击手势处理。
-(void)requireGestureRecognizerToFail:(UIGestureRecognizer*)otherGestureRecognizer;
[A requireGestureRecognizerToFail:B]它可以指定当A手势发生时,即便A已经滿足条件了,也不会立刻触发,会等到指定的手势B确定失败之后才触发。
看一个例子
-(void)viewDidLoad{
[super viewDidLoad];
UITapGestureRecognizer*tap1=[[UITapGestureRecognizer alloc]initWithTarget:self action: selector(tap1:)];
tap1.numberOfTapsRequired=1;
[self.view addGestureRecognizer:tap1];
UITapGestureRecognizer*tap2=[[UITapGestureRecognizer alloc]initWithTarget:self action: selector(tap2:)];
tap2.numberOfTapsRequired=2;
[self.view addGestureRecognizer:tap2];
//当tap2手势触发失败时才会触发tap1手势
[tap1 requireGestureRecognizerToFail:tap2];
}
-(void)tap1:(UITapGestureRecognizer*)tap
{
NSLog( "tap1手势触发");
}
-(void)tap2:(UITapGestureRecognizer*)tap
{
NSLog( "tap2手势触发");
}
3.3.UIGestureRecognizerDelegate
前面我们提到过关于手势对象的协议代理,通过代理的回调,我们可以进行自定义手势,也可以处理一些复杂的手势关系,其中方法如下:
//手指触摸屏幕后回调的方法,返回NO则不再进行手势识别,方法触发等
-(BOOL)gestureRecognizer:(UIGestureRecognizer*)gestureRecognizer shouldReceiveTouch:(UITouch*)touch;
//开始进行手势识别时调用的方法,返回NO则结束,不再触发手势
-(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer*)gestureRecognizer;
//是否支持多时候触发,返回YES,则可以多个手势一起触发方法,返回NO则为互斥
-(BOOL)gestureRecognizer:(UIGestureRecognizer*)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer*)otherGestureRecognizer;
//下面这个两个方法也是用来控制手势的互斥执行的
//这个方法返回YES,第一个手势和第二个互斥时,第一个会失效
-(BOOL)gestureRecognizer:(UIGestureRecognizer*)gestureRecognizer shouldRequireFailureOfGestureRecognizer:(UIGestureRecognizer*)otherGestureRecognizer NS_AVAILABLE_IOS(7_0);
//这个方法返回YES,第一个和第二个互斥时,第二个会失效
-(BOOL)gestureRecognizer:(UIGestureRecognizer*)gestureRecognizer shouldBeRequiredToFailByGestureRecognizer:(UIGestureRecognizer*)otherGestureRecognizer NS_AVAILABLE_IOS(7_0);
三.UIGestureRecognizer子类及子类属性
除了UIGestureRecognizer中的方法和属性是所有子类通用的之外,UIGestureRecognizer子类中分别有不同的属性和方法来对应不同的手势。
1.点击手势——UITapGestureRecognizer
点击手势十分简单,支持单击和多次点击,在我们手指触摸屏幕并抬起手指时会进行触发,其中有如下两个属性我们可以进行设置:
//设置点击次数,默认为单击
property(nonatomic)NSUInteger numberOfTapsRequired;
//设置同时点击的手指数
property(nonatomic)NSUInteger numberOfTouchesRequired;
2.捏合手势——UIPinchGestureRecognizer
捏合手势是当我们双指捏合和扩张会触发动作的手势,我们可以设置的属性如下:
//设置缩放比例
property(nonatomic)CGFloat scale;
//设置捏合速度
property(nonatomic,readonly)CGFloat velocity;
3.拖拽手势——UIPanGestureRecognzer
当我们点中视图进行慢速拖拽时会触发拖拽手势的方法。
//设置触发拖拽的最少触摸点,默认为1
property(nonatomic)NSUInteger minimumNumberOfTouches;
//设置触发拖拽的最多触摸点
property(nonatomic)NSUInteger maximumNumberOfTouches;
//获取当前位置
-(CGPoint)translationInView:(nullable UIView*)view;
//设置当前位置
-(void)setTranslation:(CGPoint)translation inView:(nullable UIView*)view;
//设置拖拽速度
-(CGPoint)velocityInView:(nullable UIView*)view;
4.滑动手势——UISwipeGestureRecognizer
滑动手势和拖拽手势的不同之处在于滑动手势更快,而拖拽比较慢。
//设置触发滑动手势的触摸点数
property(nonatomic)NSUInteger numberOfTouchesRequired;
//设置滑动方向
property(nonatomic)UISwipeGestureRecognizerDirection direction;
//枚举如下
typedef NS_OPTIONS(NSUInteger,UISwipeGestureRecognizerDirection){
UISwipeGestureRecognizerDirectionRight=1<<0,
UISwipeGestureRecognizerDirectionLeft=1<<1,
UISwipeGestureRecognizerDirectionUp=1<<2,
UISwipeGestureRecognizerDirectionDown=1<<3
};
5.旋转手势——UIRotationGestureRecognizer
进行旋转动作时触发手势方法。
//设置旋转角度
property(nonatomic)CGFloat rotation;
//设置旋转速度
property(nonatomic,readonly)CGFloat velocity;
6.长按手势——UILongPressGestureRecognizer
进行长按的时候触发的手势方法。
//设置触发前的点击次数
property(nonatomic)NSUInteger numberOfTapsRequired;
//设置触发的触摸点数
property(nonatomic)NSUInteger numberOfTouchesRequired;
//设置最短的长按时间
property(nonatomic)CFTimeInterval minimumPressDuration;
//设置在按触时时允许移动的最大距离默认为10像素
property(nonatomic)CGFloat allowableMovement;
7.自定义手势
自定义手势继承:UIGestureRecognizer,实现下面的方法,在以下方法中判断自定义手势是否实现。
–touchesBegan:withEvent:
–touchesMoved:withEvent:
–touchesEnded:withEvent:
-touchesCancelled:withEvent:
注意.m文件中需要引入#import<UIKit/UIGestureRecognizerSubclass.h>。