码迷,mamicode.com
首页 > 其他好文 > 详细

UI进阶--手势识别

时间:2015-01-06 00:36:11      阅读:218      评论:0      收藏:0      [点我收藏+]

标签:

为了完成手势识别,必须借助于手势识别器----UIGestureRecognizer
利用UIGestureRecognizer,能轻松识别用户在某个view上面做的一些常见手势
UIGestureRecognizer是一个抽象类,定义了所有手势的基本行为,使用它的子类才能处理具体的手势
UITapGestureRecognizer(敲击)
UIPinchGestureRecognizer(捏合,用于缩放)
UIPanGestureRecognizer(拖拽)
UISwipeGestureRecognizer(轻扫)
UIRotationGestureRecognizer(旋转)
UILongPressGestureRecognizer(长按)
 
手势识别的状态:
 1 typedef NS_ENUM(NSInteger, UIGestureRecognizerState) {
 2     // 没有触摸事件发生,所有手势识别的默认状态
 3     UIGestureRecognizerStatePossible,
 4     // 一个手势已经开始但尚未改变或者完成时
 5     UIGestureRecognizerStateBegan,
 6     // 手势状态改变
 7     UIGestureRecognizerStateChanged,
 8     // 手势完成
 9     UIGestureRecognizerStateEnded,
10     // 手势取消,恢复至Possible状态
11     UIGestureRecognizerStateCancelled, 
12     // 手势失败,恢复至Possible状态
13     UIGestureRecognizerStateFailed,
14     // 识别到手势识别
15     UIGestureRecognizerStateRecognized = UIGestureRecognizerStateEnded
16 };
 
每一个手势识别器的用法都差不多,比如UITapGestureRecognizer的使用步骤如下
 1 //创建手势识别器对象
 2 UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
 3 //设置手势识别器对象的具体属性
 4 // 连续敲击2次
 5 tap.numberOfTapsRequired = 2;
 6 // 需要2根手指一起敲击
 7 tap.numberOfTouchesRequired = 2;
 8 //添加手势识别器到对应的view上
 9 [self.iconView addGestureRecognizer:tap];
10 //监听手势的触发
11 [tap addTarget:self action:@selector(tapIconView:)];

 

敲击手势--UITapGestureRecognizer
①敲击手势对象相关属性
1 @property (nonatomic) NSUInteger  numberOfTapsRequired; //敲击次数,默认是1
2 @property (nonatomic) NSUInteger  numberOfTouchesRequired;//多点敲击,默认是1
②代理方法 "实现左半边才接收触摸事件"
1 - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch;//是否接受触摸事件 
 
长按手势--UILongPressGestureRecognizer
长按手势对象相关属性
1 @property (nonatomic) CFTimeInterval minimumPressDuration; //长按的时间,默认是0.5. 
2 @property (nonatomic) CGFloat allowableMovement; //触摸点周围多远的距离内长按有效,默认是50(CGPoint)
 
轻扫手势--UISwipeGestureRecognizer
轻扫手势对象相关属性
 
1 @property(nonatomic) UISwipeGestureRecognizerDirection direction; //轻扫的方向,是一个枚举型
1 typedef NS_OPTIONS(NSUInteger, UISwipeGestureRecognizerDirection) {
2     UISwipeGestureRecognizerDirectionRight = 1 << 0,//向右轻扫,默认为向右轻扫
3     UISwipeGestureRecognizerDirectionLeft  = 1 << 1,//向左轻扫
4     UISwipeGestureRecognizerDirectionUp    = 1 << 2,//向上轻扫
5     UISwipeGestureRecognizerDirectionDown  = 1 << 3//向下轻扫
6 };
 
捏合手势--UIPinchGestureRecognizer
捏合手势对象相关属性
1 @property (nonatomic)          CGFloat scale; //缩放比例,是一个累加的过程,如果想不累加,把scale设为1即可
2 //例子:
3 - (void)pinvhView:(UIPinchGestureRecognizer *)pinch {
4     self.pinView.transform = CGAffineTransformScale(self.pinView.transform,pinch.scale,pinch.scale);
5     pinch.scale = 1;
6 }
 
旋转手势--UIRotationGestureRecognizer
①旋转手势对象相关属性
1 @property (nonatomic)          CGFloat rotation;  //旋转角度,也是一个累加的过程,如果想它不累加,把其设为0即可。
2 //例子
3 - (void)rotationView:(UIRotationGestureRecognizer *)pinch {
4     self.pinView.transform = CGAffineTransformRotate(self.pinView.transform,pinch.rotation);
5     pinch.rotation = 0;
6 }
②如果捏合和旋转想同时使用,设置其中一个手势的代理并实现代理的一个方法
1 - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer;//实现该方法,返回YES即可,允许跟其它手势一起发生

 

拖拽手势--UIPanGestureRecognizer

拖拽手势对象相关方法和属性:
1 @property (nonatomic)          NSUInteger minimumNumberOfTouches;   // default is 1. the minimum number of touches required to match
2 @property (nonatomic)          NSUInteger maximumNumberOfTouches;   // default is UINT_MAX. the maximum number of touches that can be down
3 
4 - (CGPoint)translationInView:(UIView *)view;                        // translation in the coordinate system of the specified view
5 - (void)setTranslation:(CGPoint)translation inView:(UIView *)view;
6 
7 - (CGPoint)velocityInView:(UIView *)view;                           // velocity of the pan in pixels/second in the coordinate system of the specified view

 

UI进阶--手势识别

标签:

原文地址:http://www.cnblogs.com/xiaomoge/p/4204889.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!