// 手势 Gesture
// 1.轻拍Tap
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
// 设置需要点击几次才会触发方法
tap.numberOfTapsRequired = 2;
tap.numberOfTouchesRequired = 2;
// 把手势加入到图片上
[imageView addGestureRecognizer:tap];
// 内存管理
- (void)tapAction:(id)sender {
NSLog(@"taped an image");
}
// 2.长按 LongPress
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
// 设置一下长按需要的最短时间
longPress.minimumPressDuration = 3;
// 判断在长按过程中允许手指移动的距离
longPress.allowableMovement = 100;
[imageView addGestureRecognizer:longPress];
- (void)longPress:(UILongPressGestureRecognizer *)longPress {
// 长按的方法在手势的各个状态中都会进行触发,所以需要进行判断
if (longPress.state == UIGestureRecognizerStateBegan) {
NSLog(@"开始长按了");
}
NSLog(@"long pressed");
}
// 3.旋转 rotation
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationAction:)];
[imageView addGestureRecognizer:rotation];
- (void)rotationAction:(UIRotationGestureRecognizer *)rotationGesture {
// 获得添加手势的视图
UIImageView *imageView = (UIImageView *)[rotationGesture view];
// 调整视图的transform属性
imageView.transform = CGAffineTransformMakeRotation(rotationGesture.rotation);
}
// 4.捏合 pinch
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchAction:)];
[imageView addGestureRecognizer:pinch];
- (void)pinchAction:(UIPinchGestureRecognizer *)pinchGesture {
UIImageView *imageView = (UIImageView *)[pinchGesture view];
imageView.transform = CGAffineTransformMakeScale(pinchGesture.scale, pinchGesture.scale);
// imageView.transform = CGAffineTransformScale(imageView.transform, pinchGesture.scale, pinchGesture.scale);
// pinchGesture.scale = 1;
}
// 5.拖拽 pan
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];
[imageView addGestureRecognizer:pan];
- (void)panAction:(UIPanGestureRecognizer *)panGesture {
// 获取拖拽手势添加的视图
UIImageView *imageView = (UIImageView *)[panGesture view];
// 获取手势经过的点
CGPoint p = [panGesture translationInView:imageView];
// 然后对视图的transform属性进行改变
imageView.transform = CGAffineTransformMakeTranslation(p.x, p.y);
// imageView.transform = CGAffineTransformTranslate(imageView.transform, p.x, p.y);
// [panGesture setTranslation:CGPointZero inView:imageView];
}
// 6.轻扫
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeAction:)];
[imageView addGestureRecognizer:swipe];
[swipe release];
- (void)swipeAction:(UISwipeGestureRecognizer *)swipeGesture {
if (swipeGesture.direction == UISwipeGestureRecognizerDirectionLeft) {
NSLog(@"向左");
} else if (swipeGesture.direction == UISwipeGestureRecognizerDirectionRight) {
NSLog(@"向右");
} else if (swipeGesture.direction == UISwipeGestureRecognizerDirectionUp) {
NSLog(@"向上");
} else if (swipeGesture.direction == UISwipeGestureRecognizerDirectionDown) {
NSLog(@"向下");
}
}