手势UIGestureRecognizer

简介

UIGestureRecognizer继承自NSOjbect。
它提供了一些常用手势子类:

UIPanGestureRecognizer(拖动)
UIPinchGestureRecognizer(捏合)
UIRotationGestureRecognizer(旋转)
UITapGestureRecognizer(点按)
UILongPressGestureRecognizer(长按)
​UISwipeGestureRecognizer(轻扫)

也可以自行继承UIGestureRecognizer 类,实现自定义手势。

当一个控件添加了多个手势,每次事件只能识别为其中一个手势。(最先触发的手势)。例如添加了捏合和旋转手势,当这次事件起始时被判断为捏合,手不松情况下改变手捏合为滑动,系统也判断为捏合。

手势状态
UIGestureRecognizerStatePossible,   // the recognizer has not yet recognized its gesture, but may be evaluating touch events. this is the default state
UIGestureRecognizerStateBegan,      // the recognizer has received touches recognized as the gesture. the action method will be called at the next turn of the run loop
UIGestureRecognizerStateChanged,    // the recognizer has received touches recognized as a change to the gesture. the action method will be called at the next turn of the run loop
UIGestureRecognizerStateEnded,      // the recognizer has received touches recognized as the end of the gesture. the action method will be called at the next turn of the run loop and the recognizer will be reset to UIGestureRecognizerStatePossible
UIGestureRecognizerStateCancelled,  // the recognizer has received touches resulting in the cancellation of the gesture. the action method will be called at the next turn of the run loop. the recognizer will be reset to UIGestureRecognizerStatePossible
UIGestureRecognizerStateFailed,     // the recognizer has received a touch sequence that can not be recognized as the gesture. the action method will not be called and the recognizer will be reset to UIGestureRecognizerStatePossible

// Discrete Gestures – gesture recognizers that recognize a discrete event but do not report changes (for example, a tap) do not transition through the Began and Changed states and can not fail or be cancelled
UIGestureRecognizerStateRecognized = UIGestureRecognizerStateEnded // the recognizer has received touches recognized as the gesture. the action method will be called at the next turn of the run loop and the recognizer will be reset to UIGestureRecognizerStatePossible
添加手势
//给一个view添加滑动手势
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
[self.gestureView addGestureRecognizer:pan];

UIPanGestureRecognizer(拖动)

属性

@property (nonatomic)          NSUInteger minimumNumberOfTouches; //最小触摸数,如设为3,则最少3个触摸才会触发该手势。
@property (nonatomic)          NSUInteger maximumNumberOfTouches;//最大触摸数。

方法

- (CGPoint)translationInView:(nullable UIView *)view;   //得到滑动的距离
- (void)setTranslation:(CGPoint)translation inView:(nullable UIView *)view;//设置滑动的距离
- (CGPoint)velocityInView:(nullable UIView *)view; //速度

示例:

//设置一个view随手指滑动。
 -(void)handlePan:(UIPanGestureRecognizer *) pan{

   CGPoint translation = [pan translationInView:self.gestureView];
   self.gestureView.center = CGPointMake(pan.view.center.x + translation.x,
                                pan.view.center.y + translation.y);
   [pan setTranslation:CGPointMake(0, 0) inView:self.view];

}

UIPinchGestureRecognizer(捏合)

属性

@property (nonatomic)          CGFloat scale;               // scale relative to the touch points in screen coordinates
@property (nonatomic,readonly) CGFloat velocity;            // velocity of the pinch in scale/second

示例

//放大一个view
-(void)handlePinch:(UIPinchGestureRecognizer *)pinch{
   float scale = pinch.scale;
   CGPoint center = pinch.view.center;
   CGRect newframe = CGRectMake(0,0, pinch.view.frame.size.width * scale, pinch.view.frame.size.height * scale);
   pinch.view.frame = newframe;
   pinch.view.center = center;
   pinch.scale = 1.f;
}

UIRotationGestureRecognizer(旋转)

属性

@property (nonatomic)          CGFloat rotation;            //2π为一圈  rotation in radians
@property (nonatomic,readonly) CGFloat velocity;            // velocity of the pinch in radians/second

示例

//旋转一个视图
-(void)handleRotation:(UIRotationGestureRecognizer *)rotation{
   rotation.view.transform = CGAffineTransformRotate(rotation.view.transform,rotation.rotation);
   rotation.rotation = 0;
}

​UISwipeGestureRecognizer(轻扫)

属性

@property(nonatomic) NSUInteger                        numberOfTouchesRequired ; // default is 1. the number of fingers that must swipe
@property(nonatomic) UISwipeGestureRecognizerDirection direction;               // default is UISwipeGestureRecognizerDirectionRight. the desired direction of the swipe. multiple directions may be specified if they will result in the same behavior (for example, UITableView swipe delete)

示例

//添加一个3个手指以上的向上或者向下轻扫手势,
UISwipeGestureRecognizer * swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
[swipe setDirection:UISwipeGestureRecognizerDirectionDown|UISwipeGestureRecognizerDirectionUp];//设置向上或者向下触发。
swipe.numberOfTouchesRequired = 3;//需要3个触碰才能触发
[self.gestureView addGestureRecognizer:swipe];

UITapGestureRecognizer(点按)

@property (nonatomic) NSUInteger  numberOfTapsRequired;       // Default is 1. The number of taps required to match
@property (nonatomic) NSUInteger  numberOfTouchesRequired __TVOS_PROHIBITED;    // Default is 1. The number of fingers required to match

示例同上

UILongPressGestureRecognizer(长按)

属性

@property (nonatomic) NSUInteger numberOfTapsRequired;      // Default is 0. The number of full taps required before the press for gesture to be recognized
@property (nonatomic) NSUInteger numberOfTouchesRequired __TVOS_PROHIBITED;   // Default is 1. Number of fingers that must be held down for the gesture to be recognized

@property (nonatomic) CFTimeInterval minimumPressDuration; // Default is 0.5. Time in seconds the fingers must be held down for the gesture to be recognized
@property (nonatomic) CGFloat allowableMovement;           // Default is 10. Maximum movement in pixels allowed before the gesture fails. Once recognized (after minimumPressDuration) there is no limit on finger movement for the remainder of the touch tracking

示例

// 设置最少按5秒,可以手指移动100像素点以内。
UILongPressGestureRecognizer * lang = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLong:)];
lang.minimumPressDuration = 5;
lang.allowableMovement =100;

多手势识别

当一个view上添加多个手势时,可能会互相影响。例如Swipe与 Pan,单击与双击。这样就需要一个手势判断失败另一个才能成功。例如双击判断失败才触发单击。或者Swipe判断失败才触发Pan。

UITapGestureRecognizer * tap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap1:)];
tap1.numberOfTapsRequired = 1;
UITapGestureRecognizer * tap2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap2:)];
tap2.numberOfTapsRequired = 2;
[tap1 requireGestureRecognizerToFail:tap2]; //关键代码。

[self.gestureView addGestureRecognizer:tap1];
[self.gestureView addGestureRecognizer:tap2];

你可能感兴趣的:(手势UIGestureRecognizer)