UIGestureRecognizer:
手势处理器,用来处理用户触碰事件。
有这几个子类:
- UITapGestureRecognizer : 处理点击手势
- UILongPressGestureRecognizer: 处理长按手势
- UIPinchGestureRecognizer: 处理捏合手势
- UIRotationGestureRecognizer: 处理旋转手势
- UISwipeGestureRecognizer: 处理滑动手势
- UIPanGestureRecognizer: 处理拖动手势
使用处理器步骤:
- 根据程序需要创建对应的手势处理器对象。(需要指定target和action参数)
- 设置控件允许用户交互。(有的控件默认不允许用户交互,如:UILable、UIImageView,则将该控件userInteractionEnabled属性设为YES;如果希望控件支持多点触碰,则将multipleTouchEnabled设为YES)。
- 为控件添加手势处理器。(调用控件的addGestureRecognizer:方法添加手势处理器。)
UIGestureRecognizer常用属性和方法:
@property(nonatomic, getter=isEnabled) BOOL enabled; // default is YES 用于设置和返回处理器是否可用。
@property(nonatomic,readonly) UIGestureRecognizerState state; // the current state of the gesture recognizer
@property(nullable, nonatomic,readonly) UIView *view;//返回激发该手势的UI控件。
- (NSUInteger)numberOfTouches;//返回该手势包含触碰点数,即几个手指进行触碰。
- (CGPoint)locationInView:(nullable UIView*)view;//返回该手势在view控件中的触碰位置
- (CGPoint)locationOfTouch:(NSUInteger)touchIndex inView:(nullable UIView*)view;//返回该手势中第几个触碰点在view控件中的位置。