UIGestureRecognizer手势 : 1-概要介绍
UIKit框架中一共提供了6种类型的手势操作,分别为:点击、捏合、旋转、滑动、拖动、长按。手势需要添加到UIView类以及其子类之上,同时每个UIView可以添加多个手势。
UIGestureRecognizer类简介
UIGestureRecognizer类是一个抽象类,不能直接使用,具体使用过程中需要使用其子类。UIGestureRecognizer一共包括6个子类,分别对应6种手势,即:
- UITapGestureRecognizer:点击
- UIPinchGestureRecognizer:捏合
- UIRotationGestureRecognizer:旋转
- UISwipeGestureRecognizer:滑动
- UIPanGestureRecognizer:拖动,其又包含另外一个子类:UIScreenEdgePanGestureRecognizer
- UILongPressGestureRecognizer:长按
UIGestureRecognizer创建方法
创建一个UIGestureRecognizer子类对象需要有三个步骤:
- 使用- initWithTarget:action:方法创建一个手势类对象,设置当手势事件发生时,调用的方法;
- 使用 addGestureRecognizer:方法,把手势对象添加到一个UIView的对象上;
- 实现手势事件发生时所调用的方法。
UIGestureRecognizer类常用属性以及方法
UIGestureRecognizer类虽然是一个抽象类,但是其中定义了其子手势类中可以通用的方法和属性。下面的几个方法是需要程序员重点掌握的。
- 初始化手势对象并设置手势发生时调用的方法
– (instancetype)initWithTarget:(nullable id)target action:(nullable SEL)action;
- 获取手势点击的位置
– (CGPoint)locationOfTouch:(NSUInteger)touchIndex inView:(nullable UIView*)view;
- 获取手势点击的状态和视图
@property(nonatomic,readonly) UIGestureRecognizerState state;
其中:常用的UIGestureRecognizerState有:
- UIGestureRecognizerStateBegan:开始手势事件
- UIGestureRecognizerStateEnded:结束手势事件
- UIGestureRecognizerStateChanged:手势位置发生变化
- UIGestureRecognizerStateFailed:无法识别的手势
- 手势事件对应的视图对象
@property(nullable, nonatomic,readonly) UIView *view;
文章发布时间为: August 8th , 2016 at 08:15 pm
最后编辑时间为: September 13th , 2017 at 02:55 pm
本文由 99ios 创作,转载请注明出处
最后编辑时间为: September 13th , 2017 at 02:55 pm
本文由 99ios 创作,转载请注明出处