Cowboy Tech

iOS项目13-手势识别

手势的种类

  1. UITapGestureRecognizer
  2. UIPinGestureRecognizer Pinch 捏合
  3. UIRotationGestureRecognizer Rotation 旋转
  4. UISwipeGestureRecognizer Swipe 轻扫,快速移动,是用于监测滑动的方向
  5. UIPanGestureRecognizer Pan 拖移,慢速移动,是用于监测偏移量
  6. UILongPressGestureRecognizer LongPress 长按

Tap点击

/*  一个手指单击*/
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(SignleTap:)];
tap.numberOfTapsRequired = 1;
tap.numberOfTouchesRequired = 1;
[self.view addGestureRecognizer:tap];

/*一个手指双击*/
UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(DoubleTap:)];
tap2.numberOfTapsRequired = 2;
[self.view addGestureRecognizer:tap2];

//当tap 和 tap2同时出现在视图上,取消tap
[tap requireGestureRecognizerToFail:tap2];

/* 两个手指单击*/
UITapGestureRecognizer *tap3 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(SigleTwoFingerTap:)];
tap3.numberOfTouchesRequired = 2;
[self.view addGestureRecognizer:tap3];
//当tap2 和 tap3同时出现在视图上,取消tap2
[tap2 requireGestureRecognizerToFail:tap3];

Tap互斥

/*
[A requireGestureRecognizerToFail:B]
当A手势发生时,即便A已经满足条件,但要等到B手势失败后才触发
*/

Swipe轻扫手势

UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(SwipeAction:)];
swipe.direction = UISwipeGestureRecognizerDirectionLeft;
[self.view addGestureRecognizer:swipe];

Pinch捏合手势

- (void)PinchGesture{
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(PinchAction:)];
[self.view addGestureRecognizer:pinch];
}

//pin的缩放
- (void)PinchAction:(UIPinchGestureRecognizer *)pinch{
float scale = pinch.scale;
pinch.view.transform = CGAffineTransformScale(pinch.view.transform, scale, scale);
if (scale > 1) {
    _genstureLabel.text = @"捏合放大";
}else if (scale < 1){
    _genstureLabel.text = @"捏合缩小";
}
}

Pan拖移手势

- (void)PanGesture{
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(PanAction:)];
[_genstureLabel addGestureRecognizer:pan];
}

- (void)PanAction:(UIPanGestureRecognizer *)pan{
//移动的坐标值
CGPoint translation = [pan translationInView:self.view];
//移动后的坐标
pan.view.center = CGPointMake(pan.view.center.x + translation.x, pan.view.center.y + translation.y);
//设置坐标和速度
[pan setTranslation:CGPointZero inView:self.view];
_genstureLabel.text = @"把我放哪啊";
}

Rotation旋转手势

- (void)RotationGesture{
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(RotationAction:)];
[_genstureLabel addGestureRecognizer:rotation];
}

- (void)RotationAction:(UIRotationGestureRecognizer *)rotationGes{  
rotationGes.view.transform = CGAffineTransformRotate(rotationGes.view.transform, rotationGes.rotation);
rotationGes.rotation = M_PI;
_genstureLabel.text = @"旋转";
}

LongPress长按手势

- (void)LongPressGesture{
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(LongPressAction:)];
longPress.minimumPressDuration = 2;
[self.view addGestureRecognizer:longPress];  
}