1 #import "ViewController.h"
2 @interface ViewController () <UIGestureRecognizerDelegate>
4 @property (weak, nonatomic) IBOutlet UIImageView *imageView;
5 @end
7 @implementation ViewController
8 - (void)viewDidLoad {
9 [super viewDidLoad];
10 //只要是手势,默认都是相对于原来位置,且默认控制只支持一个手势!!若想支持多个手势,详见下面的代理方法
11 // [self setUpRotation];
12 // [self setUpPinch];
13 [self setUpPan];
14 }
15
16 #pragma mark - 点按手势
17 - (void)setUpTap
18 {
19 //创建点按手势
20 UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
21 //需要连续点3次才能触发
22 // tap.numberOfTapsRequired = 3;
23 //往imageView上添加点按手势
24 [self.imageView addGestureRecognizer:tap];
25 //设置点按手势代理
26 tap.delegate = self;
27 }
28
29 - (void)tap:(UITapGestureRecognizer *)tap
30 {
31 NSLog(@"%s",__func__);
32 }
33
34 #pragma mark - 长按手势
35 //默认触发两次(开始触摸半秒后一次,结束触摸一次)
36 - (void)setUpLongPress
37 {
38 UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
39 [self.imageView addGestureRecognizer:longPress];
40 longPress.delegate = self;
41 }
42
43 - (void)longPress:(UILongPressGestureRecognizer *)longPress
44 {
45 开始的时候触摸
46 if (longPress.state == UIGestureRecognizerStateBegan) {
47 NSLog(@"%s",__func__);
48 }
49 结束的时候触摸
50 if (longPress.state == UIGestureRecognizerStateEnded) {
51 NSLog(@"%s",__func__);
52 }
53 }
54
55 #pragma mark - 轻扫手势
56 - (void)setUpSwipe
57 {
58 //默认轻扫手势往右划屏幕
59 UISwipeGestureRecognizer *swipeUp = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];
60 //向上划
61 swipeUp.direction = UISwipeGestureRecognizerDirectionUp;
62 [_imageView addGestureRecognizer:swipeUp];
63 //一个清扫手势只支持一个方向,如果想支持多个方向,必须创建多个
64 UISwipeGestureRecognizer *swipeDown = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];
65 //向下划
66 swipeDown.direction = UISwipeGestureRecognizerDirectionDown;
67 [_imageView addGestureRecognizer:swipeDown];
68 }
69 - (void)swipe:(UISwipeGestureRecognizer *)swipe
70 {
71 NSLog(@"%s",__func__);
72 }
73
74 #pragma mark - 旋转手势
75 //默认传递的角度都相对于最开始的位置
76 - (void)setUpRotation
77 {
78 UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotation:)];
79 rotation.delegate = self;
80 [self.imageView addGestureRecognizer:rotation];
81 }
82 - (void)rotation:(UIRotationGestureRecognizer *)rotation
83 {
84 //让图片随着手势旋转而旋转,旋转的角度相当于初始位置的值,配合rotation.rotation = 0;无法转动
85 // self.imageView.transform = CGAffineTransformMakeRotation(rotation.rotation);
86 //手势旋转一下能让图片高速旋转N圈,并且不受控制,方向不容易确定。配合rotation.rotation = 0;后角度一直为0
87 self.imageView.transform = CGAffineTransformRotate(self.imageView.transform, rotation.rotation);
88 //复位
89 rotation.rotation = 0;
90 //获取手势旋转的角度
91 NSLog(@"%f",rotation.rotation);
92 }
93
94 #pragma mark - 捏合手势
95 //两根手指放大缩小图片
96 - (void)setUpPinch
97 {
98 UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinch:)];
99 pinch.deleg