首页 > 代码库 > 六种手势

六种手势

1.点击

UITapGestureRecognizer *tapGes = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapGR:)];

    tapGes.numberOfTouchesRequired = 1;

    tapGes.numberOfTapsRequired = 2;

    [self.view addGestureRecognizer:tapGes];

  

- (void)tapGR:(UIGestureRecognizer *)Gesture{

    NSLog(@"点击手势");

    UIView *v = Gesture.view;

    

    v.backgroundColor = [UIColor greenColor];

    

    

}

 

2.拖拽

    UIPanGestureRecognizer *panGes = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panView:)];  

    [_panView addGestureRecognizer:panGes];

}

- (void)panView:(UIGestureRecognizer *)Gesture{

    NSLog(@"pan");

    CGPoint viewP;

    CGPoint panP;

    if (Gesture.state == UIGestureRecognizerStateBegan) {

        viewP = _panView.center;

        panP  = [Gesture locationInView:self.view];

        

    }

    

    CGPoint nowP = [Gesture locationInView:self.view];

    float x = nowP.x - panP.x;

    float y = nowP.y - panP.y;

    

    _panView.center = CGPointMake(viewP.x + x, viewP.y + y);

    

}

3.轻扫

//    UISwipeGestureRecognizer *swipeGes = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeGR:)];

//    swipeGes.numberOfTouchesRequired = 1;//几个手指

//    swipeGes.direction = UISwipeGestureRecognizerDirectionUp;

//    [self.view addGestureRecognizer:swipeGes];

    

    //四个方向的手势

    for (int i = 0; i<4; i++) {

        UISwipeGestureRecognizer *swipeGes = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeGR:)];

        swipeGes.numberOfTouchesRequired = 1;//几个手指

        swipeGes.direction = 1<<i;

        [self.view addGestureRecognizer:swipeGes];

        

    }

    

}

 

- (void)swipeGR:(UISwipeGestureRecognizer *)gesture{

    NSLog(@"swipe");

    NSInteger dir = gesture.direction;

    NSString *str;

    if (dir == 1<<0 ? str = @"向上": @"") {

        NSLog(@"%@",str);

    }

    

}

4.长按

UILongPressGestureRecognizer *longGes = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longGes:)];

    

    longGes.minimumPressDuration = 1;

    

    [self.view addGestureRecognizer:longGes];

}

- (void)longGes:(UIGestureRecognizer *)gesture{

    

    NSLog(@"longpress");

}

5.旋转

  UIRotationGestureRecognizer *rotationGes = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotationGes:)];

    

    

    [self.view addGestureRecognizer:rotationGes];

    

}

 

//

- (void)rotationGes:(UIRotationGestureRecognizer *)gesture{

    NSLog(@"rotaiton");

    static CGFloat startRot;

    

    self.view.transform = CGAffineTransformMakeRotation(gesture.rotation + startRot);

 

    if (gesture.state == UIGestureRecognizerStateEnded) {

        startRot += gesture.rotation;

    }

    

 

}

6.捏合(放大/缩小)

  UIPinchGestureRecognizer *pinchGes = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinchGes:)];

    

    [self.view addGestureRecognizer:pinchGes];

    

}

- (void)pinchGes:(UIPinchGestureRecognizer *)gesture{

    NSLog(@"pinch");

    static CGFloat startScale = 1;

    

    self.view.transform = CGAffineTransformMakeScale(gesture.scale * startScale, gesture.scale * startScale);

    

    if (gesture.state == UIGestureRecognizerStateEnded) {

        startScale *= gesture.scale;

    }

    

}

六种手势