GestureRecognizer手势识别

轻触

- (void)addTapEvent
{
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
    [self.imageView addGestureRecognizer:tapGesture];
}

- (void)tapAction:(UITapGestureRecognizer*)gesture
{
    NSLog(@"tap");
}

长按

- (void)addLongPressEvent
{
    UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressAction:)];
    [self.imageView addGestureRecognizer:longPressGesture];
}

- (void)longPressAction:(UILongPressGestureRecognizer*)gesture
{
    NSLog(@"long press");

    /** gesture.state取值
     typedef NS_ENUM(NSInteger, UIGestureRecognizerState) {
     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
     };
     **/
    if(gesture.state == UIGestureRecognizerStateBegan)
    {
        NSLog(@"开始长按");
    }
    else if(gesture.state == UIGestureRecognizerStateChanged)
    {
        NSLog(@"开始长按并移动");
    }
    else if(gesture.state == UIGestureRecognizerStateEnded)
    {
        NSLog(@"结束长按");
    }
}

轻扫

- (void)addSwipeEvent
{
    //一个轻扫手势只能支持1个方向,如果需要其他方向,就需要再注册一个UISwipeGestureRecognizer
    UISwipeGestureRecognizer *swipeGesture1 = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeAction:)];
    swipeGesture1.direction = UISwipeGestureRecognizerDirectionRight;
    [self.imageView addGestureRecognizer:swipeGesture1];

    UISwipeGestureRecognizer *swipeGesture2 = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeAction:)];
    swipeGesture2.direction = UISwipeGestureRecognizerDirectionLeft;
    [self.imageView addGestureRecognizer:swipeGesture2];

    UISwipeGestureRecognizer *swipeGesture3 = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeAction:)];
    swipeGesture3.direction = UISwipeGestureRecognizerDirectionUp;
    [self.imageView addGestureRecognizer:swipeGesture3];

    UISwipeGestureRecognizer *swipeGesture4 = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeAction:)];
    swipeGesture4.direction = UISwipeGestureRecognizerDirectionDown;
    [self.imageView addGestureRecognizer:swipeGesture4];

}

- (void)swipeAction:(UISwipeGestureRecognizer*)gesture
{
    NSLog(@"swipe");
    /** gesture.direction 取值
     UISwipeGestureRecognizerDirectionRight = 1 << 0,
     UISwipeGestureRecognizerDirectionLeft  = 1 << 1,
     UISwipeGestureRecognizerDirectionUp    = 1 << 2,
     UISwipeGestureRecognizerDirectionDown  = 1 << 3
     **/

    if(gesture.direction == UISwipeGestureRecognizerDirectionRight)
    {
        NSLog(@"右扫");
    }
    else if(gesture.direction == UISwipeGestureRecognizerDirectionLeft)
    {
        NSLog(@"左扫");
    }
    else if(gesture.direction == UISwipeGestureRecognizerDirectionUp)
    {
        NSLog(@"上扫");
    }
    else if(gesture.direction == UISwipeGestureRecognizerDirectionDown)
    {
        NSLog(@"下扫");
    }
}

移动

- (void)addPanEvent
{
    UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panTranslationAction:)];
    //UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panLocalAction:)];
    [self.view addGestureRecognizer:panGesture];
}

- (void)panTranslationAction:(UIPanGestureRecognizer*)gesture
{
    //translationInView:获取到的是手指移动后,在相对手指刚按下去的位置的偏移量
    CGPoint tranPoint = [gesture translationInView:self.imageView];
    NSLog(@"tranPoint:%@",NSStringFromCGPoint(tranPoint));

    //imageView手指按住的点跟随手指移动
    self.imageView.transform = CGAffineTransformTranslate(self.imageView.transform, tranPoint.x, tranPoint.y);
    //偏移量复位
    [gesture setTranslation:CGPointZero inView:self.imageView];
}

- (void)panLocalAction:(UIPanGestureRecognizer*)gesture
{
    //locationInView:获取到的是手指点击屏幕实时的坐标点;
    CGPoint localPoint = [gesture locationInView:self.view];
    NSLog(@"localPoint:%@",NSStringFromCGPoint(localPoint));

    //imageView中心跟随手指移动
    [self.imageView setCenter:localPoint];
}

捏合

- (void)addPinchEvent
{
    UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinchAction:)];
    [self.imageView addGestureRecognizer:pinchGesture];
}

- (void)pinchAction:(UIPinchGestureRecognizer *)gesture
{
    self.imageView.transform = CGAffineTransformScale(self.imageView.transform, gesture.scale, gesture.scale);
    //偏移量复位
    [gesture setScale:1];
}

旋转

- (void)addRotationEvent
{
    UIRotationGestureRecognizer *rotationGesture = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotationAction:)];
    [self.imageView addGestureRecognizer:rotationGesture];
}

- (void)rotationAction:(UIRotationGestureRecognizer *)gesture
{
    self.imageView.transform = CGAffineTransformRotate(self.imageView.transform, gesture.rotation);
    //偏移量复位
    [gesture setRotation:0];
}

如果要多个手势同时有效,需要实现UIGestureRecognizerDelegate代理方法

...
pinchGesture.delegate = self;
...

...
rotationGesture.delegate = self;
...

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return YES;
}

results matching ""

    No results matching ""