初始化

– (instancetype)initWithTarget:(nullable id)target action:(nullable SEL)action;

或者

UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] init];

[pan addTarget:self action:@selector(panGesture:)];

 

 

如果这样

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
     UITapGestureRecognizer * ges = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(click:)];
    [ges addTarget:self action:@selector(haha)];
     [self.view addGestureRecognizer:ges];
}
-(void)click:(UIGestureRecognizer *)ges{
     
    NSLog(@"第一个手势的触发方法");
     
}
-(void)haha{
    NSLog(@"haha");
}
两个方法都会被执行

 

//NS_CLASS_AVAILABLE_IOS(3_2) @interface UIGestureRecognizer : NSObject
//
//// Valid action method signatures:
////     -(void)handleGesture;
////     -(void)handleGesture:(UIGestureRecognizer*)gestureRecognizer;
//创建一个手势
  - (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; // remove the specified target/action pair. passing nil for target matches all targets, and the same for actions
//
//获取手势的状态
  @property(nonatomic,readonly) UIGestureRecognizerState state;  // the current state of the gesture recognizer
//
//代理
  @property(nullable,nonatomic,weak) id <UIGestureRecognizerDelegate> delegate; // the gesture recognizer\'s delegate
//
//手势是否有效
  @property(nonatomic, getter=isEnabled) BOOL enabled;  // default is YES. disabled gesture recognizers will not receive touches. when changed to NO the gesture recognizer will be cancelled if it\'s currently recognizing a gesture
//
//// a UIGestureRecognizer receives touches hit-tested to its view and any of that view\'s subviews
//手势所在的view
  @property(nullable, nonatomic,readonly) UIView *view;           // the view the gesture is attached to. set by adding the recognizer to a UIView using the addGestureRecognizer: method
//
//touchBegan等等叫触摸,Gesture叫手势
//取消发送触摸消息,就是不掉用touchBegan等等方法
  @property(nonatomic) BOOL cancelsTouchesInView;       // default is YES. causes touchesCancelled:withEvent: or pressesCancelled:withEvent: to be sent to the view for all touches or presses recognized as part of this gesture immediately before the action method is called.
//是否延时掉用TouchesBegan,如果为yes,手势识别失败才会调用TouchesBegan。如果为no,不管手势识别成不成功,都会调用TouchesBegan。
  @property(nonatomic) BOOL delaysTouchesBegan;         // default is NO.  causes all touch or press events to be delivered to the target view only after this gesture has failed recognition. set to YES to prevent views from processing any touches or presses that may be recognized as part of this gesture
// 这个 没有明白。是否延时掉用TouchesEnded ???没什么效果啊。。
  @property(nonatomic) BOOL delaysTouchesEnded;         // default is YES. causes touchesEnded or pressesEnded events to be delivered to the target view only after this gesture has failed recognition. this ensures that a touch or press that is part of the gesture can be cancelled if the gesture is recognized
//
//存储已经发生的手势状态值,。
  @property(nonatomic, copy) NSArray<NSNumber *> *allowedTouchTypes NS_AVAILABLE_IOS(9_0); // Array of UITouchType\'s as NSNumbers.
  @property(nonatomic, copy) NSArray<NSNumber *> *allowedPressTypes NS_AVAILABLE_IOS(9_0); // Array of UIPressTypes as NSNumbers.
//
//// create a relationship with another gesture recognizer that will prevent this gesture\'s actions from being called until otherGestureRecognizer transitions to UIGestureRecognizerStateFailed
//// if otherGestureRecognizer transitions to UIGestureRecognizerStateRecognized or UIGestureRecognizerStateBegan then this recognizer will instead transition to UIGestureRecognizerStateFailed
//// example usage: a single tap may require a double tap to fail
//第一个参数是需要失效的手势,第二个是生效的手势。
  - (void)requireGestureRecognizerToFail:(UIGestureRecognizer *)otherGestureRecognizer;
//
//// individual UIGestureRecognizer subclasses may provide subclass-specific location information. see individual subclasses for details
//获取触电的位置
  - (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
//获取某一个触点的位置(因为iphone是多点触控。)
  - (CGPoint)locationOfTouch:(NSUInteger)touchIndex inView:(nullable UIView*)view; // the location of a particular touch
//
  @end

以下来自http://my.oschina.net/u/2340880/blog/527077

零 UIGestureRecognizerDelegate

        前面我们提到过关于手势对象的协议代理,通过代理的回调,我们可以进行自定义手势,也可以处理一些复杂的手势关系,其中方法如下:

1
2
3
4
5
6
7
8
9
10
11
//手指触摸屏幕后回调的方法,返回NO则不再进行手势识别,方法触发等
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch;
//开始进行手势识别时调用的方法,返回NO则结束,不再触发手势
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer;
//是否支持多时候触发,返回YES,则可以多个手势一起触发方法,返回NO则为互斥
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer;
//下面这个两个方法也是用来控制手势的互斥执行的
//这个方法返回YES,第一个手势和第二个互斥时,第一个会失效
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRequireFailureOfGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer NS_AVAILABLE_IOS(7_0);
//这个方法返回YES,第一个和第二个互斥时,第二个会失效
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldBeRequiredToFailByGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer NS_AVAILABLE_IOS(7_0);

 

 

一、点击手势——UITapGestureRecognizer

        点击手势十分简单,支持单击和多次点击,在我们手指触摸屏幕并抬起手指时会进行触发,其中有如下两个属性我们可以进行设置:

1
2
3
4
//设置点击次数,默认为单击
@property (nonatomic) NSUInteger  numberOfTapsRequired; 
//设置同时点击的手指数
@property (nonatomic) NSUInteger  numberOfTouchesRequired;

二、捏合手势——UIPinchGestureRecognizer

        捏合手势是当我们双指捏合和扩张会触发动作的手势,我们可以设置的属性如下:

1
2
3
4
//设置缩放比例
@property (nonatomic)          CGFloat scale; 
//设置捏合速度
@property (nonatomic,readonly) CGFloat velocity;

三、拖拽手势——UIPanGestureRecognzer

        当我们点中视图进行慢速拖拽时会触发拖拽手势的方法。

1
2
3
4
5
6
7
8
9
10
//设置触发拖拽的最少触摸点,默认为1
@property (nonatomic)          NSUInteger minimumNumberOfTouches; 
//设置触发拖拽的最多触摸点
@property (nonatomic)          NSUInteger maximumNumberOfTouches;  
//获取当前位置
- (CGPoint)translationInView:(nullable UIView *)view;            
//设置当前位置
- (void)setTranslation:(CGPoint)translation inView:(nullable UIView *)view;
//设置拖拽速度
- (CGPoint)velocityInView:(nullable UIView *)view;

四、滑动手势——UISwipeGestureRecognizer

        滑动手势和拖拽手势的不同之处在于滑动手势更快,拖拽比较慢。

1
2
3
4
5
6
7
8
9
10
11
//设置触发滑动手势的触摸点数
@property(nonatomic) NSUInteger                        numberOfTouchesRequired; 
//设置滑动方向
@property(nonatomic) UISwipeGestureRecognizerDirection direction;  
//枚举如下
typedef NS_OPTIONS(NSUInteger, UISwipeGestureRecognizerDirection) {
    UISwipeGestureRecognizerDirectionRight = 1 << 0,
    UISwipeGestureRecognizerDirectionLeft  = 1 << 1,
    UISwipeGestureRecognizerDirectionUp    = 1 << 2,
    UISwipeGestureRecognizerDirectionDown  = 1 << 3
};

五、旋转手势——UIRotationGestureRecognizer

        进行旋转动作时触发手势方法。

1
2
3
4
//设置旋转角度
@property (nonatomic)          CGFloat rotation;
//设置旋转速度 
@property (nonatomic,readonly) CGFloat velocity;

 六、长按手势——UILongPressGestureRecognizer

        进行长按的时候触发的手势方法。

1
2
3
4
5
6
7
8
//设置触发前的点击次数
@property (nonatomic) NSUInteger numberOfTapsRequired;    
//设置触发的触摸点数
@property (nonatomic) NSUInteger numberOfTouchesRequired; 
//设置最短的长按时间
@property (nonatomic) CFTimeInterval minimumPressDuration; 
//设置在按触时时允许移动的最大距离 默认为10像素

 

 

版权声明:本文为kinghx原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/kinghx/p/5311593.html