/** 手势的使用 */
1. 点击
几根手指 点击几次 触发手势
- (void)addTap {
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
点击 几次 触发手势
tap.numberOfTapsRequired = 1;
// 几个手指 点击触发手势
tap.numberOfTouchesRequired = 2;
tap.delegate = self;
[self.mainImageView addGestureRecognizer:tap];
}
- (void)tapAction:(UITapGestureRecognizer *)tap {
NSLog(@"%s", __FUNCTION__);
}
2.长按手势
- (void)addLongPress {
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
// 设置长按触发的最小时间
longPress.minimumPressDuration = 2.0f;
// 用户手指在长按过程中允许移动的距离
longPress.allowableMovement = 200;
[self.mainImageView addGestureRecognizer:longPress];
}
- (void)longPress:(UILongPressGestureRecognizer *)longPress {
长按 longPress.minimumPressDuration 这么长时间之后调用
if (longPress.state == UIGestureRecognizerStateBegan) {
NSLog(@"%s", __FUNCTION__);
}
// 长按结束(抬起手指后)调用
if (longPress.state == UIGestureRecognizerStateEnded) {
NSLog(@"111%s", __FUNCTION__);
}
}
3. 旋转手势, 别忘了复位
- (void)addRotation {
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotation:)];
rotation.delegate = self;
[self.mainImageView addGestureRecognizer:rotation];
}
- (void)rotation:(UIRotationGestureRecognizer *)rotation {
self.mainImageView.transform = CGAffineTransformRotate(self.mainImageView.transform, rotation.rotation);
复位
rotation.rotation = 0;
}
4. 捏合
- (void)addPinch {
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinch:)];
// 因为要同时支持多个手势
pinch.delegate = self;
[self.mainImageView addGestureRecognizer:pinch];
}
- (void)pinch:(UIPinchGestureRecognizer *)pinch {
self.mainImageView.transform = CGAffineTransformScale(self.mainImageView.transform, pinch.scale, pinch.scale);
复位
pinch.scale = 1;
}
5. 拖拽
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizeralloc] initWithTarget:selfaction:@selector(panAction:)]; //创建
[self.imageViewaddGestureRecognizer:pan];// 添加到图片
[pan release]; //释放
- (void)panAction:(UIPanGestureRecognizer *)pan {
UIImageView *imageView = (UIImageView *)pan.view; //通过手势找视图
CGPoint point = [pan translationInView:imageView]; //获得手势经过的点
imageView.transform = CGAffineTransformTranslate(imageView.transform, point.x, point.y); //设置移动的位置
[pan setTranslation:CGPointZeroinView:imageView];// 为了防止手势在操作的时候消失
}
6. 轻扫手势
- (void)addSwipe {
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];
向左扫
swipe.direction = UISwipeGestureRecognizerDirectionLeft;
[self.mainImageView addGestureRecognizer:swipe];
}
- (void)swipe:(UISwipeGestureRecognizer *)swipe {
if (swipe.direction == UISwipeGestureRecognizerDirectionLeft) {
NSLog(@"%s", __FUNCTION__);
}
}
默认是不支持多个手势, 当你使用一个手势的时候就会调用
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return YES;
}
这个触摸点能否触发手势
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
CGPoint currentPoint = [touch locationInView:self.mainImageView];
if (currentPoint.x < self.mainImageView.bounds.size.width * 0.5) { // 左边 不可以 触发手势
return NO;
}else{ // 右边 可以 触发手势
return YES;
}
}