码迷,mamicode.com
首页 > 移动开发 > 详细

IOS中的手势识别

时间:2015-12-20 00:41:10      阅读:310      评论:0      收藏:0      [点我收藏+]

标签:

  最近做项目需要用到手势识别,所以花了点时间去学习了一下,其实手势识别这部分的内容不多,还是很容易上手的。

  IOS中的手势一共有六种 :tap(点击),longPress(长按),swipe(挥动),pan(拖动),pich(缩放),rotation(旋转)。这六个手势类都是继承自UIGestureRecongnizer,所以,先来看看UIGestureRecongnizer这个类。

  UIGestureRecongizer继承自NSObject,是以上六种手势的父类,定义了一些所有手势公用的属性和方法,下面条一些比较常用的。

初始化方法:

- (instancetype)initWithTarget:(nullable id)target action:(nullable SEL)action NS_DESIGNATED_INITIALIZER; // designated initializer
- (void)addTarget:(id)target action:(SEL)action;   // add a target/action pair. you can call this multiple times to specify multiple target/actions
- (void)removeTarget:(nullable id)target action:(nullable SEL)action;

- (CGPoint)locationInView:(nullable UIView*)view;                                // a generic single-point location for the gesture. usually the centroid of the touches involved
- (NSUInteger)numberOfTouches;                                          // number of touches involved for which locations can be queried
- (CGPoint)locationOfTouch:(NSUInteger)touchIndex inView:(nullable UIView*)view; // the location of a particular touch

以上的方法都比较容易理解,看方法名就知道是什么意思了,值得一提的是代理方法:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer;

和实例方法:- (void)requireGestureRecognizerToFail:(UIGestureRecognizer *)otherGestureRecognizer;

这两个方法是用于当两个手势冲突时(比如同事添加swipe和pan时)解决冲突的。首先实现以上代理方法,返回YES,然后调用requireGestureRecognizerToFail:方法如下:

[pan requireGestureRecognizerToFail:swipe];

这样当pan和swipe手势冲突时,会优先执行swipe手势,只有在swipe手势识别失败后才会执行pan手势。

属性:

@property(nonatomic,readonly) UIGestureRecognizerState state;//定义了该手势当前状态,是开始,结束,还是失败等等。

@property(nullable, nonatomic,readonly) UIView *view;//该手势的父视图

 

tap点击手势:UITapGestureRecognizer

最简单的手势,只有两个属性

@property (nonatomic) NSUInteger  numberOfTapsRequired;//需要点击下才会响应

@property (nonatomic) NSUInteger  numberOfTouchesRequired;//需要几个点才回响应

 

longPress长按手势:UILongPressGestureRecognizer

@property (nonatomic) NSUInteger numberOfTouchesRequired //需要多少个点按着
@property (nonatomic) CFTimeInterval minimumPressDuration; //需要按多久
@property (nonatomic) CGFloat allowableMovement;//按的时候手指的移动范围

 

swipe挥动手势 UISwipeGestureRecognizer

初始化一个UISwipeGestureRecognizer手势后,要先设置它的direction属性,设定它的方向。一个swipe手势智能响应一个方向,如果需要多个方向的挥动动作,那么久需要添加多个swipe手势。

@property(nonatomic) UISwipeGestureRecognizerDirection direction;//方向

@property(nonatomic) NSUInteger numberOfTouchesRequired //需要几个点才回响应

 

pan拖动手势 UIPanGestureRecognizer

- (CGPoint)translationInView:(nullable UIView *)view; // 手指在视图上移动的位置
- (void)setTranslation:(CGPoint)translation inView:(nullable UIView *)view;// 设置手指在视图上移动的位置
- (CGPoint)velocityInView:(nullable UIView *)view;  // 手指在视图上移动的速率

 

pinch缩放手势 UIPinchGestureRecognizer

@property (nonatomic)   CGFloat scale; // 缩放后的比例
@property (nonatomic,readonly) CGFloat velocity;  // 缩放的速率

 

rotation旋转手势 UIRotationGestureRecognizer

@property (nonatomic)  CGFloat rotation; // rotation in radians 旋转的弧度
@property (nonatomic,readonly) CGFloat velocity;  // 旋转的速率(弧度/秒)

 

IOS中的手势识别

标签:

原文地址:http://www.cnblogs.com/chuangege/p/5060027.html

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