UIGestureRecognizer手势识别

UIGestureRecognizer

1.#import "ViewController.h"
2.
3.@interface ViewController ()<UIGestureRecognizerDelegate>
4.{
5. UIImageView *imageView ;
6. NSInteger flag;
7.}
8.
9.@property (nonatomic,strong) UIView *panView;
10.
11.@end
12.
13.@implementation ViewController
14.
15.- (void)viewDidLoad {
16. [super viewDidLoad];
17.
18. UIImage *image = [UIImage imageNamed:@"DSC00592.JPG"];
19.
20. CGFloat interval = (self.view.frame.size.width - 240) / 2.0;
21.
22. imageView = [[UIImageView alloc] init];
23. imageView.frame = CGRectMake(interval, 230, 240, 160);
24.
25. imageView.image = image;
26.
27. imageView.userInteractionEnabled = YES;
28.
29. // 捏合
30. UIPinchGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc] init];
31. [pinchRecognizer addTarget:self action:@selector(pinch:)];
32. [imageView addGestureRecognizer:pinchRecognizer];
33. pinchRecognizer.delegate = self;//给手势识别器添加委托
34.
35. // 旋转
36. UIRotationGestureRecognizer *rotationRecognizer = [[UIRotationGestureRecognizer alloc]
37. initWithTarget:self action:@selector(rotate:)];
38. [imageView addGestureRecognizer:rotationRecognizer];
39. rotationRecognizer.delegate = self;//给手势识别器添加委托
40.
41. // 点击
42. UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc]
43. initWithTarget:self action:@selector(tap:)];
44. tapRecognizer.numberOfTapsRequired = 3;//设置连续点击三次才响应
45. [imageView addGestureRecognizer:tapRecognizer];
46.
47. // 长按
48. UILongPressGestureRecognizer *longPressRecognizer = [[UILongPressGestureRecognizer alloc]
49. initWithTarget:self action:@selector(longPress:)];
50. [self.view addGestureRecognizer:longPressRecognizer];
51.
52.
53. // 滑动 swipe
54.// UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];
55.// swipe.direction = UISwipeGestureRecognizerDirectionRight; // 每个对象只能监听一个方向
56.//
57.// [self.view addGestureRecognizer:swipe];
58.
59. //若需要监听不同方向的滑动那么需要不同懂swipe对象
60.
61. for (int i = 0; i < 4; i++) {
62. // 四个方向
63. UISwipeGestureRecognizerDirection direction[] = {
64. UISwipeGestureRecognizerDirectionDown,
65. UISwipeGestureRecognizerDirectionLeft,
66. UISwipeGestureRecognizerDirectionUp,
67. UISwipeGestureRecognizerDirectionRight
68. };
69. UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];
70. swipe.direction = direction[i];
71.
72. [self.view addGestureRecognizer:swipe];
73. }
74.
75. // 拖动 pan
76.
77. _panView = [[UIView alloc] initWithFrame:CGRectMake(30, 100, 30, 30)];
78. _panView.backgroundColor = [UIColor purpleColor];
79. [self.view addSubview:_panView];
80.
81. UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
82. [_panView addGestureRecognizer:pan];
83.
84.
85. [self.view addSubview:imageView];
86.
87.}
88.
89.-(void)pinch:(UIPinchGestureRecognizer *)recognizer{
90.
91. imageView.transform = CGAffineTransformScale(imageView.transform,
92. recognizer.scale,
93. recognizer.scale);
94. // 每次捏合之后都要将scale变为1,防止叠加
95. recognizer.scale = 1;
96.}
97.
98.-(void)rotate:(UIRotationGestureRecognizer *)recognizer{
99.
100. imageView.transform = CGAffineTransformRotate(imageView.transform, recognizer.rotation);
101.
102. // 每次旋转后都要将rotation变为0,防止叠加
103. recognizer.rotation = 0;
104.}
105.
106.-(void)tap:(UITapGestureRecognizer *)recognizer{
107.
108. [imageView removeFromSuperview];
109.
110.}
111.
112.-(void)longPress:(UILongPressGestureRecognizer *)recognizer{
113.
114. // 只有在刚监测到长按时才绘制
115. if (recognizer.state == UIGestureRecognizerStateBegan) {
116. UIView *view = [[UIView alloc] initWithFrame:CGRectMake(random() % 300, rand() % 600, 50, 50)];
117. view.backgroundColor = [UIColor orangeColor];
118. CGPoint point = [recognizer locationInView:self.view];//获取长按的位置
119. view.center = point;
120. [self.view addSubview:view];
121. }
122.
123.}
124.
125.-(void)swipe:(UISwipeGestureRecognizer *)recognizer{
126. switch (recognizer.direction) {
127. case UISwipeGestureRecognizerDirectionUp:{
128. NSLog(@"up");
129. }
130. break;
131.
132. case UISwipeGestureRecognizerDirectionLeft:{
133. NSLog(@"left");
134. }
135. break;
136. case UISwipeGestureRecognizerDirectionDown:{
137. NSLog(@"down");
138. }
139. break;
140. case UISwipeGestureRecognizerDirectionRight:{
141. NSLog(@"right");
142. }
143. break;
144. default:
145. break;
146. }
147.}
148.
149.-(void)pan:(UIPanGestureRecognizer *)recognizer{
150. CGPoint point = [recognizer locationInView:self.view];
151. _panView.center = point;
152.}
153.
154.
155.
156.// 返回YES使得两个手势操作可以同时实现 (委托方法)
157.- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
158. return YES;
159.}
160.
161.
162.
163.@end
164.
 

转载于:https://www.cnblogs.com/buakaw/p/5208293.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值