你真的了解UIGestureRecognizer吗?
一:首先查看一下关于UIGestureRecognizer的定义
//当前手势状态 typedef NS_ENUM(NSInteger, UIGestureRecognizerState) { //尚未识别是何种手势操作(但可能已经触发了触摸事件),默认状态 UIGestureRecognizerStatePossible, //手势已经开始,此时已经被识别,但是这个过程中可能发生变化,手势操作尚未完成 UIGestureRecognizerStateBegan, //手势状态发生改变 UIGestureRecognizerStateChanged, // 手势识别操作完成(此时已经松开手指) UIGestureRecognizerStateEnded, //手势被取消,恢复到默认状态 UIGestureRecognizerStateCancelled, //手势识别失败,恢复到默认状态 UIGestureRecognizerStateFailed, //手势识别完成,同end UIGestureRecognizerStateRecognized = UIGestureRecognizerStateEnded }; NS_CLASS_AVAILABLE_IOS(3_2) @interface UIGestureRecognizer : NSObject //创建一个手势对象并添加触发事件 - (instancetype)initWithTarget:(nullable id)target action:(nullable SEL)action NS_DESIGNATED_INITIALIZER; //给一个手势对象添加监听事件 - (void)addTarget:(id)target action:(SEL)action; //移除一个手势的监听事件 - (void)removeTarget:(nullable id)target action:(nullable SEL)action; //获取当前手势状态 @property(nonatomic,readonly) UIGestureRecognizerState state; //委托 @property(nullable,nonatomic,weak) id <UIGestureRecognizerDelegate> delegate; //手势识别是否可用 @property(nonatomic, getter=isEnabled) BOOL enabled; //获取手势触摸的View视图 只读 @property(nullable, nonatomic,readonly) UIView *view; //是否取消触摸控件的响应 默认为YES,这种情况下当手势识别器识别到触摸之后,会发送touchesCancelled给触摸到的控件以取消控件view对touch的响应,这个时候只有手势识别器响应touch,当设置成NO时,手势识别器识别到触摸之后不会发送touchesCancelled给控件,这个时候手势识别器和控件view均响应touch。 注意:手势识别和触摸事件是同时存在的,只是因为touchesCancelled导致触摸事件失效、 @property(nonatomic) BOOL cancelsTouchesInView; //是否延迟发送触摸事件给触摸到的控件 默认是NO,这种情况下当发生一个触摸时,手势识别器先捕捉到到触摸,然后发给触摸到的控件,两者各自做出响应。如果设置为YES,手势识别器在识别的过程中(注意是识别过程),不会将触摸发给触摸到的控件,即控件不会有任何触摸事件。只有在识别失败之后才会将触摸事件发给触摸到的控件,这种情况下控件view的响应会延迟约0.15ms。 @property(nonatomic) BOOL delaysTouchesBegan; //如果触摸识别失败是否立即结束本次手势识别的触摸事件 @property(nonatomic) BOOL delaysTouchesEnded; //指定一个手势需要另一个手势执行失败才会执行,同时触发多个手势使用其中一个手势的解决办法 有时手势是相关联的,如单机和双击,点击和长按,点下去瞬间可能只会识别到单击无法识别其他,该方法可以指定某一个 手势,即便自己已经满足条件了,也不会立刻触发,会等到该指定的手势确定失败之后才触发 - (void)requireGestureRecognizerToFail:(UIGestureRecognizer *)otherGestureRecognizer; //获取当前触摸在指定视图上的点 - (CGPoint)locationInView:(nullable UIView*)view; //获取触摸手指数 - (NSUInteger)numberOfTouches; //多指触摸的触摸点相对于指定视图的位置 - (CGPoint)locationOfTouch:(NSUInteger)touchIndex inView:(nullable UIView*)view; @end
UIGestureRecognizer是一个抽象类,定义了所有手势的基本行为,使用它的子类才能处理具体的手势
知识点1:关于UIGestureRecognizer的子类如下(下面这些才是我们平常会直接运用到的类):
UITapGestureRecognizer(轻触,点按)
UILongPressGestureRecognizer(长按)
UISwipeGestureRecognizer(轻扫手势)
UIRotationGestureRecognizer(旋转手势)
UIPanGestureRecognizer(拖拽手势)
UIPinchGestureRecognizer(捏合手势,缩放用)
实例如下:
//拖拽手势 UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)]; [tagButton addGestureRecognizer:pan]; - (void)pan:(UIPanGestureRecognizer *)pan { }
//给视图增加点击动作 UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapHandle:)]; [self.view addGestureRecognizer:tap]; -(void)tapHandle:(UITapGestureRecognizer*)tap { }
二:关于UIGestureRecognizerDelegate代理的内容
@protocol UIGestureRecognizerDelegate <NSObject> @optional //开始进行手势识别时调用的方法,返回NO则结束识别,不再触发手势,用处:可以在控件指定的位置使用手势识别 - (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer; //是否支持多手势触发,返回YES,则可以多个手势一起触发方法,返回NO则为互斥 是否允许多个手势识别器共同识别,一个控件的手势识别后是否阻断手势识别继续向下传播,默认返回NO;如果为YES,响应者链上层对象触发手势识别后,如果下层对象也添加了手势并成功识别也会继续执行,否则上层对象识别后则不再继续传播 - (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); //手指触摸屏幕后回调的方法,返回NO则不再进行手势识别,方法触发等 此方法在window对象在有触摸事件发生时,调用gesture recognizer的touchesBegan:withEvent:方法之前调用,如果返回NO,则gesture recognizer不会看到此触摸事件。(默认情况下为YES) - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch; @end
知识点1:
//是否同时支持多种手势 - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{ return YES; } //是否允许开始点击 - (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{ return YES; } //设置点击的范围 - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{ //获取当前的触摸点 CGPoint curp = [touch locationInView:self.imageView]; if (curp.x <= self.imageView.bounds.size.width*0.5) { return NO; }else{ return YES; } }
知识点2:UITapGestureRecognizer和UIButton的点击事件冲突的解决办法
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{ if ([touch.view isKindOfClass:[UIButton class]]) { return NO; } return YES; }
三:关于UIGestureRecognizer的子类知识
1:UITapGestureRecognizer(轻触,点按)
NS_CLASS_AVAILABLE_IOS(3_2) @interface UITapGestureRecognizer : UIGestureRecognizer //设置能识别到手势的最少的轻触次数(默认为1) @property (nonatomic) NSUInteger numberOfTapsRequired; //设置能识别到手势的最少的手指的个数(默认为1) @property (nonatomic) NSUInteger numberOfTouchesRequired; @end
知识点1:实例代码如下
// 创建一个手势对象 UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)]; // 设置能识别到手势的最少的轻触次数 tap.numberOfTapsRequired = 3; // 设置能识别到手势的最少的手指的个数 tap.numberOfTouchesRequired = 2; //把手势对象添加到对应的控件中 [self.imgView addGestureRecognizer:tap];
2:UILongPressGestureRecognizer(长按手势)
NS_CLASS_AVAILABLE_IOS(3_2) @interface UILongPressGestureRecognizer : UIGestureRecognizer //设置能识别到手势的最少的轻触次数(默认为1) @property (nonatomic) NSUInteger numberOfTapsRequired; //设置能识别到手势的最少的手指的个数(默认为1) @property (nonatomic) NSUInteger numberOfTouchesRequired; //设置能识别到长按手势的最短的长按时间,单位:秒,默认为0.5 @property (nonatomic) CFTimeInterval minimumPressDuration; //设置长按时允许移动的最大距离,单位:像素,默认为10像素 @property (nonatomic) CGFloat allowableMovement; @end
知识点1:实例代码如下
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction:)]; // 设置能识别到长按手势的最小的长按时间 longPress.minimumPressDuration = 0.5; // "容错的范围" longPress.allowableMovement = 10; // 把长按手势添加到对应的控件中 [self.imgView addGestureRecognizer:longPress];
3:UISwipeGestureRecognizer(轻扫手势)
typedef NS_OPTIONS(NSUInteger, UISwipeGestureRecognizerDirection) { UISwipeGestureRecognizerDirectionRight = 1 << 0, //向右滑 UISwipeGestureRecognizerDirectionLeft = 1 << 1, //向左滑 UISwipeGestureRecognizerDirectionUp = 1 << 2, //向上滑 UISwipeGestureRecognizerDirectionDown = 1 << 3 //向下滑 }; NS_CLASS_AVAILABLE_IOS(3_2) @interface UISwipeGestureRecognizer : UIGestureRecognizer //最少触摸手指个数,默认为1 @property(nonatomic) NSUInteger numberOfTouchesRequired; //设置轻扫手势支持的方向,默认为向右滑 @property(nonatomic) UISwipeGestureRecognizerDirection direction; @end
知识点1:实例代码如下
UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeAction:)]; swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft; [self.imgView addGestureRecognizer:swipeLeft];
4:UIRotationGestureRecognizer(旋转手势)
NS_CLASS_AVAILABLE_IOS(3_2) @interface UIRotationGestureRecognizer : UIGestureRecognizer //旋转的角度 @property (nonatomic) CGFloat rotation; //旋转速度,单位:度/秒、 @property (nonatomic,readonly) CGFloat velocity; @end
知识点1:实例代码如下
//为图片框添加一个旋转手势 UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotateAction:)];rotation.delegate = self; [self.imgView addGestureRecognizer:rotation]; // 旋转手势的监听方法 - (void)rotateAction:(UIRotationGestureRecognizer *)recognizer { // 在原来的基础上, 累加多少度 recognizer.view.transform = CGAffineTransformRotate(recognizer.view.transform, recognizer.rotation); // 每次旋转完毕后将rotation的值, 恢复到0的位置.recognizer.rotation = 0; }
5:UIPanGestureRecognizer(拖拽手势)
NS_CLASS_AVAILABLE_IOS(3_2) @interface UIPanGestureRecognizer : UIGestureRecognizer //设置触发拖拽最少手指数,默认为1 @property (nonatomic) NSUInteger minimumNumberOfTouches; //设置触发拖拽最多手指数,默认为 UINT_MAX 无限大 @property (nonatomic) NSUInteger maximumNumberOfTouches; //获取当前拖拽位置 - (CGPoint)translationInView:(nullable UIView *)view; //设置当前拖拽位置 - (void)setTranslation:(CGPoint)translation inView:(nullable UIView *)view; //设置拖拽速度,单位:像素/秒 - (CGPoint)velocityInView:(nullable UIView *)view; @end
知识点1:实例代码
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)]; [self.imgView addGestureRecognizer:pan]; // 拖拽手势的监听方法 - (void)panAction:(UIPanGestureRecognizer *)recognizer { // 1. 获取手指拖拽的时候, 平移的值 CGPoint translation = [recognizer translationInView:recognizer.view]; // 2. 让当前控件做响应的平移 recognizer.view.transform = CGAffineTransformTranslate(recognizer.view.transform, translation.x, translation.y); // 3. 每次平移手势识别完毕后, 让平移的值不要累加 [recognizer setTranslation:CGPointZero inView:recognizer.view]; }
6:UIPinchGestureRecognizer(捏合手势,缩放用)
NS_CLASS_AVAILABLE_IOS(3_2) @interface UIPinchGestureRecognizer : UIGestureRecognizer //设置缩放比例 @property (nonatomic) CGFloat scale; //获取捏合速度,单位:缩放比/秒 @property (nonatomic,readonly) CGFloat velocity; @end
知识点1:实例代码
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchAction:)]; pinch.delegate = self; [self.imgView addGestureRecognizer:pinch]; // 捏合手势监听方法 - (void)pinchAction:(UIPinchGestureRecognizer *)recognizer { recognizer.view.transform = CGAffineTransformScale(recognizer.view.transform, recognizer.scale, recognizer.scale); recognizer.scale = 1.0; }