1.手势UIGestureRecognizer
UIGestureRecognizer是一个抽象类我们不能直接使用,他有6个子类(tap,pan,swip,long,scale,raition)。他的父类NSObject。
2.常用属性和函数
//函数
创建-(instancetype)initWithTarget:target action(SEL)action单独添加target方法 可以添加多个手势-(void)addTarget:target action:(SEL)action删除手势-(void)removeTarget:(id)target action:(SEL)action- (void)requireGestureRecognizerToFail:(UIGestureRecognizer *)otherGestureRecognizer; //手势依赖(手势互斥)方法 设置手势优先级 A requireRecognizerToFail B 手势条件都满足时B触发,A不会//获取在传入view的点击位置的信息方法- (CGPoint)locationInView:(nullable UIView*)view;//(touchIndex 是第几个触摸点)用来获取多触摸点在view上位置信息的方法- (CGPoint)locationOfTouch:(NSUInteger)touchIndex inView:(nullable UIView*)view;常用属性
UIGestureRecognizerState state; 手势状态 只读//手势状态枚举值typedef NS_ENUM(NSInteger, UIGestureRecognizerState) {UIGestureRecognizerStatePossible, // 默认的状态,这个时候的手势并没有具体的情形状态UIGestureRecognizerStateBegan, // 手势开始被识别的状态UIGestureRecognizerStateChanged, // 手势识别发生改变的状态UIGestureRecognizerStateEnded, // 手势识别结束,将会执行触发的方法UIGestureRecognizerStateCancelled, // 手势识别取消UIGestureRecognizerStateFailed, // 识别失败,方法将不会被调用UIGestureRecognizerStateRecognized = UIGestureRecognizerStateEndedid <UIGestureRecognizerDelegate> delegate;代理BOOL enabled 手势是否有效 默认YESUIview *view 获取手势所在的view 只读BOOL cancelsTouchesInView 取消view上面的touch事件响应 默认YES 就是拦截了,手势触发了就不会发送touchesCancelled:withEvent:消息BOOL delaysTouchsBegan 延迟touch事件开始 默认NO
BOOL delaysTouchsEnded 延迟touch事件结束 默认YES
NSArray<NSNumber *> *allowedTouchTypes 允许touch类型的数组
NSArray<NSNumber *> *allowedPressTypes 允许按压press的类型数组
BOOL requiresExclusiveTouchType 是否只允许一种touchType 类型,是否同时只接受一种触摸类型 默认YES
NSUInteger numberOfTouches; 获取触摸点数代理方法开始进行手势识别时调用,返回NO,手势识别失败-(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer;手势触发屏幕后回调方法,返回NO手势识别失败-(BOOL)getureRecognizer:(uiGestureRecognizer*)gestureRecognizer;
2.子类类型
UITapGestureRecognizer 点击
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapclack)];//点击次数 默认单击tapGesture.numberOfTapsRequired = 2;//同时点击的手指数// tapGesture.numberOfTouchesRequired = 2;[self.imageView addGestureRecognizer:tapGesture];-(void)tapclack{NSLog(@"点击了");}
UIPinchGestureRecognizer 捏合
//属性 CGFloat scale 缩放比例// CGFloat velocty 设置捏合速度 只读UIPinchGestureRecognizer *pingGest = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinClack:)];[self.imageView addGestureRecognizer:pingGest];-(void)pinClack:(UIPinchGestureRecognizer *)pin{NSLog(@"%f,%f",pin.scale,pin.velocity);//固定写法pin.view.transform = CGAffineTransformScale(pin.view.transform, pin.scale, pin.scale);//重置缩放系数(否则系数会累加)pin.scale = 1.0;
}
UIRotationGestureRecognizer 旋转
-(void)rotation{
//属性 CGFloat rotation 旋转角度
//旋转速度 CGFloat velocity 只读UIRotationGestureRecognizer *rotationGest = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotationClack:)];[self.imageView addGestureRecognizer:rotationGest];}-(void)rotationClack:(UIRotationGestureRecognizer *)rotationGest{rotationGest.view.transform = CGAffineTransformRotate(rotationGest.view.transform,rotationGest.rotation);NSLog(@"旋转角度%f",rotationGest.rotation);}
UISwipeGestureRecognizer 轻扫
-(void)swipe{//常用属性 NSInteger numberOfTouchesRequired 触发轻扫手势所需触摸点数量//UISwipeGestureRecognizerDirection direction 轻扫手势方向//左UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipClack:)];//轻扫方向swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;[self.imageView addGestureRecognizer:swipeLeft];//右UISwipeGestureRecognizer *swipRight = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipClack:)];swipRight.direction = UISwipeGestureRecognizerDirectionRight;[self.imageView addGestureRecognizer:swipRight];//上UISwipeGestureRecognizer *swipUp = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipClack:)];swipUp.direction = UISwipeGestureRecognizerDirectionUp;[self.imageView addGestureRecognizer:swipUp];//下UISwipeGestureRecognizer *swipDrown = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipClack:)];swipDrown.direction = UISwipeGestureRecognizerDirectionDown;[self.imageView addGestureRecognizer:swipDrown];
}-(void)swipClack:(UISwipeGestureRecognizer *)swipe{switch (swipe.direction) {case UISwipeGestureRecognizerDirectionLeft:NSLog(@"左");break;case UISwipeGestureRecognizerDirectionRight:NSLog(@"右");break;case UISwipeGestureRecognizerDirectionUp:NSLog(@"上");break;case UISwipeGestureRecognizerDirectionDown:NSLog(@"下");break;default:break;}}
UIPanGestureRecognizer 滑动
-(void)pan{/*常用属性NSUInteger minimumNumberOfTouchches 设置触发移动拖拽最少触发点数量 默认1NSUInteger maximumNumberOfTouches 设置触发拖拽最多触发点-(void)setTranslation:(CGPoint)translation inView(UIView *)view 设置当前位置-(CGPoint)translationInView:(UIView)view 获取当前位置-(void)setVelocityInView:(UIView *)view 设置平移速度-(CGPoint)velocityInView:(UIView *)view 获取平移速度*/UIPanGestureRecognizer *panGest = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panClack:)];[self.imageView addGestureRecognizer:panGest];}-(void)panClack:(UIPanGestureRecognizer *)pan{//获取imageview在self.view上的便宜量CGPoint point = [pan translationInView:self.view];NSLog(@"%f--%f",point.x,point.y);//移动到的中心点 本来中心点+便宜量CGPoint newCenter = CGPointMake(pan.view.center.x + point.x, pan.view.center.y+point.y);//设置拖动范围newCenter.x = MAX(pan.view.frame.size.width,newCenter.x);//最小范围//newCenter.x = MIN();//没错调用后,重之手势便宜量[pan setTranslation:CGPointZero inView:self.imageView];if (pan.state == UIGestureRecognizerStateBegan) {NSLog(@"开始响应");}else if (pan.state == UIGestureRecognizerStateChanged){NSLog(@"手势状态发生改变");}else{NSLog(@"手势结束");}}
UILongPressGestureRecognizer 长按
-(void)LongPress{/*属性NSUInteger numberOfTapsRequired。 设置触发前的点击次数NSUInteger numberOfTouchesReauired 设置触发的触摸点数CGTimeInterval minimumPressDuration 设置最短长按时间CGFloat allowableMovement 设置在按触时允许移动的最大距离,默认10像素*/UILongPressGestureRecognizer *longPressGest = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressClack:)];//触摸点 默认1longPressGest.numberOfTapsRequired = 1;//设置最短长按时间 秒 默认 0.5longPressGest.minimumPressDuration = 1;//设置手势识别期间所允许的手势可移动范围//longPressGest.allowableMovement = 10;[self.imageView addGestureRecognizer:longPressGest];}
-(void)longPressClack:(UILongPressGestureRecognizer *)longpress{if (longpress.state == UIGestureRecognizerStateBegan) {NSLog(@"长按手势开始响应");}else if (longpress.state == UIGestureRecognizerStateChanged){NSLog(@"长按手势状态发生改变");}else{NSLog(@"长按手势结束");}}
3.解决手势冲突
- (void)requireGestureRecognizerToFail:(UIGestureRecognizer *)otherGestureRecognizer; //手势依赖(手势互斥)方法 设置手势优先级 A requireRecognizerToFail B 手势条件都满足时B触发,A不会