iOS:手势的详解UIGestureReconizer
手势类:UIGestureReconizer
typedef NS_ENUM(NSInteger, UIGestureRecognizerState) {
UIGestureRecognizerStatePossible, //评估可能发生的手势状态
UIGestureRecognizerStateBegan, //手势开始状态
UIGestureRecognizerStateChanged, //手势持续状态
UIGestureRecognizerStateEnded, //手势结束状态
UIGestureRecognizerStateCancelled, //手势取消状态
UIGestureRecognizerStateFailed, //手势失败状态
UIGestureRecognizerStateRecognized = UIGestureRecognizerStateEnded ; //确认的手势结束状态
};
@interface UIGestureRecognizer : NSObject
属性:
//手势状态
@property(nonatomic,readonly) UIGestureRecognizerState state;
//手势代理
@property(nonatomic,assign) id <UIGestureRecognizerDelegate> delegate;
//能否手势
@property(nonatomic, getter=isEnabled) BOOL enabled;
//手势触摸的视图
@property(nonatomic,readonly) UIView *view;
//是否取消触摸视图
@property(nonatomic) BOOL cancelsTouchesInView;
// 是否延迟触摸开始
@property(nonatomic) BOOL delaysTouchesBegan;
// 是否延迟触摸结束
@property(nonatomic) BOOL delaysTouchesEnded;
方法:
//初始化,创建手势,并响应事件
- (instancetype)initWithTarget:(id)target action:(SEL)action;
// 添加手势事件
- (void)addTarget:(id)target action:(SEL)action;
//移除手势事件
- (void)removeTarget:(id)target action:(SEL)action;
//指定一个手势失败,另一个手势才执行
- (void)requireGestureRecognizerToFail:(UIGestureRecognizer *)otherGestureRecognizer;
//返回当前触摸点坐标系
- (CGPoint)locationInView:(UIView*)view;
//返回触摸点个数
- (NSUInteger)numberOfTouches;
//返回指定视图中第几个触摸点的坐标系
- (CGPoint)locationOfTouch:(NSUInteger)touchIndex inView:(UIView*)view;
@end
手势的代理协议
@protocol UIGestureRecognizerDelegate <NSObject>
@optional
//询问一个手势接收者是否应该开始解释执行一个触摸接收事件
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer;
//询问delegate,两个手势是否同时接收消息,返回YES同事接收。返回NO,不同是接收(如果另外一个手势返回YES,则并不能保证不同时接收消息)the default implementation returns NO。这个函数一般在一个手势接收者要阻止另外一个手势接收自己的消息的时候调用
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer;
//询问是否另外一个手势失败,这个手势才开始执行
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRequireFailureOfGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer ;
//询问是否这个手势是通过一个指定手势的触发才失败
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldBeRequiredToFailByGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer ;
// 询问delegate是否允许手势接收者接收一个touch对, 返回YES,则允许对这个touch对象审核,NO,则不允许。这个方法在touchesBegan:withEvent:之前调用,为一个新的touch对象进行调用
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch;
@end
@interface UITapGestureRecognizer : UIGestureRecognizer {
属性:
//点击次数,默认为1次
@property (nonatomic) NSUInteger numberOfTapsRequired;
//需要触摸的手指个数,默认为1个
@property (nonatomic) NSUInteger numberOfTouchesRequired;
@end
=====================================================
2、pinch捏合手势:放缩
@interface UIPinchGestureRecognizer : UIGestureRecognizer {
属性:
//放缩系数
@property (nonatomic) CGFloat scale;
//每秒放缩的速率
@property (nonatomic,readonly) CGFloat velocity;
@end
=====================================================
3、rotate旋转手势:旋转
@interface UIRotationGestureRecognizer : UIGestureRecognizer {
属性:
//旋转弧度
@property (nonatomic) CGFloat rotation;
//每秒旋转的速率
@property (nonatomic,readonly) CGFloat velocity;
@end
=====================================================
4、pan拖动手势:拖动
@interface UIPanGestureRecognizer : UIGestureRecognizer {
属性:
//需要的最少触摸手指数,默认为1
@property (nonatomic) NSUInteger minimumNumberOfTouches;
//需要的最多触摸手指数,默认为unit_max
@property (nonatomic) NSUInteger maximumNumberOfTouches;
//返回在指定视图中的坐标系
- (CGPoint)translationInView:(UIView *)view;
//设置在指定视图中的坐标系(可以用于复位操作)
- (void)setTranslation:(CGPoint)translation inView:(UIView *)view;
//返回拖动手势的速度,值是每秒移动过的坐标系point
- (CGPoint)velocityInView:(UIView *)view;
@end
=====================================================
5、longPress长按手势:长时间按下
@interface UILongPressGestureRecognizer : UIGestureRecognizer {
属性:
//需要按的次数,默认为0
@property (nonatomic) NSUInteger numberOfTapsRequired;
//需要触摸的手指数,默认为1
@property (nonatomic) NSUInteger numberOfTouchesRequired;
//长按的最小时间,默认为0.5秒
@property (nonatomic) CFTimeInterval minimumPressDuration;
//允许移动的唯一,默认为10
@property (nonatomic) CGFloat allowableMovement;
@end
=====================================================
6、swipe轻扫手势:轻轻扫过
轻扫方向枚举(位移运算,可以与或非)
typedef NS_OPTIONS(NSUInteger, UISwipeGestureRecognizerDirection) {
UISwipeGestureRecognizerDirectionRight = 1 << 0, //往右扫
UISwipeGestureRecognizerDirectionLeft = 1 << 1, //往左扫
UISwipeGestureRecognizerDirectionUp = 1 << 2, //向上扫
UISwipeGestureRecognizerDirectionDown = 1 << 3 //向下扫
};
@interface UISwipeGestureRecognizer : UIGestureRecognizer {
属性:
//需要触摸的手指数,默认为1
@property(nonatomic) NSUInteger numberOfTouchesRequired;
//手势轻扫的方向
@property(nonatomic) UISwipeGestureRecognizerDirection direction;
@end
===================================================
7、摇动手势
![](https://images2015.cnblogs.com/blog/791499/201510/791499-20151009220125565-197122743.png)
![](https://images2015.cnblogs.com/blog/791499/201510/791499-20151009220201456-790746158.png)
![](https://images2015.cnblogs.com/blog/791499/201510/791499-20151009220210315-80806986.png)
//一、tap手势(点击) //1.创建手势识别对象 UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap:)]; //2.设置属性 tap.numberOfTapsRequired = 1; //需要按几次 tap.numberOfTouchesRequired = 1; //需要几个点(手指)按 //3.把手势添加到视图中 [self.myView addGestureRecognizer:tap];
执行:
//tap手势识别的处理方法 -(void)tap:(UITapGestureRecognizer *)sender { UIView *view = sender.view; if ([view.backgroundColor isEqual:[UIColor redColor]]) { view.backgroundColor = [UIColor greenColor]; } else { view.backgroundColor = [UIColor redColor]; } }
![](https://images2015.cnblogs.com/blog/791499/201510/791499-20151009221734768-1156394101.png)
![](https://images2015.cnblogs.com/blog/791499/201510/791499-20151009221740596-1821157271.png)
//二、pinch手势(放大、缩小) UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinch:)]; [self.view addGestureRecognizer:pinch];
//pinch手势识别的处理方法 -(void)pinch:(UIPinchGestureRecognizer *)sender { if(sender.state == UIGestureRecognizerStateChanged) { //使用放射变换来放大和缩小 CGAffineTransform transform = self.myView.transform; transform = CGAffineTransformScale(transform, sender.scale, sender.scale); self.myView.transform = transform; } //复位 sender.scale = 1.0; }
演示结果:(大图缩小成小图)
//三、rotate旋转的手势 UIRotationGestureRecognizer *rotate = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotate:)]; [self.view addGestureRecognizer:rotate];
执行:
//rotate手势识别的处理方法 -(void)rotate:(UIRotationGestureRecognizer *)sender { //持续性操作 if (sender.state == UIGestureRecognizerStateChanged) { //使用放射变换进行旋转 //self.myView.transform = CGAffineTransformMakeRotation(sender.rotation);(不用复位) CGAffineTransform transform = self.myView.transform; transform = CGAffineTransformRotate(transform, sender.rotation); self.myView.transform = transform; } //复位 sender.rotation = 0; }
演示结果:(旋转了)
//四、pan拖动的手势 UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(pan:)]; //需要手指数 pan.minimumNumberOfTouches = 1; pan.maximumNumberOfTouches = 1; [self.view addGestureRecognizer:pan];
//pan手势识别的处理方法 -(void)pan:(UIPanGestureRecognizer *)sender { //持续性操作 if (sender.state == UIGestureRecognizerStateChanged) { CGPoint offsetPoint = [sender translationInView:self.view]; CGPoint center = self.myView.center; self.myView.center = CGPointMake(center.x+offsetPoint.x, center.y+offsetPoint.y); } //复位 [sender setTranslation:CGPointZero inView:self.view]; }
演示结果:(从中间拖到了右下角)
//五、longPress长按手势 UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPress:)]; //需要按的次数 longPress.numberOfTapsRequired = 1; //需要几根手指 longPress.numberOfTouchesRequired = 1; //按住的最小时间 longPress.minimumPressDuration = 0.2f; [self.myView addGestureRecognizer:longPress];
//longPress手势识别的处理方法 -(void)longPress:(UILongPressGestureRecognizer *)sender { //持续性操作 if (sender.state == UIGestureRecognizerStateChanged) { self.myView.backgroundColor = [UIColor yellowColor]; } else if (sender.state == UIGestureRecognizerStateEnded) { self.myView.backgroundColor = [UIColor redColor]; } }
演示结果:(长按时间到,红色变为黄色)
//六、swipe轻扫手势 UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipe:)]; //设置手势轻扫方向 swipe.direction = UISwipeGestureRecognizerDirectionUp; [self.view addGestureRecognizer:swipe];
//swipe手势识别的处理方法 -(void)swipe:(UISwipeGestureRecognizer *)sender { if (sender.direction == UISwipeGestureRecognizerDirectionRight) { NSLog(@"swipe Right"); } else if (sender.direction == UISwipeGestureRecognizerDirectionLeft) { NSLog(@"swipe Left"); } else if (sender.direction == UISwipeGestureRecognizerDirectionDown) { NSLog(@"swipe Down"); } else if (sender.direction == UISwipeGestureRecognizerDirectionUp) { NSLog(@"swipe Up"); } } @end
演示结果:(只有向上扫,才输出结果)
2015-10-09 22:23:27.818 01-手势识别gestureReconizer[6332:352467] swipe Up
<7>摇动手势
重写方法:
-(void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event { if (event.subtype == UIEventSubtypeMotionShake) { NSLog(@"摇一摇,摇到外婆桥"); } }
运行结果:
2015-10-09 22:37:35.757 02-shake[6438:358883] 摇一摇,摇到外婆桥
摇动手势操作方法,点击硬件设备上的如下Shake Gesture按钮即可: