iOS 手势操作:拖动、捏合、旋转、点按、长按、轻扫、自定义
作者:网络转载 发布时间:[ 2015/8/6 11:31:42 ] 推荐标签:移动开发 移动测试
30 - (void)didReceiveMemoryWarning {
31 [super didReceiveMemoryWarning];
32 // Dispose of any resources that can be recreated.
33 }
34
35 #pragma mark - 处理手势操作
36 /**
37 * 处理拖动手势
38 *
39 * @param recognizer 拖动手势识别器对象实例
40 */
41 - (void)handlePan:(UIPanGestureRecognizer *)recognizer {
42 //视图前置操作
43 [recognizer.view.superview bringSubviewToFront:recognizer.view];
44
45 CGPoint center = recognizer.view.center;
46 CGFloat cornerRadius = recognizer.view.frame.size.width / 2;
47 CGPoint translation = [recognizer translationInView:self.view];
48 //NSLog(@"%@", NSStringFromCGPoint(translation));
49 recognizer.view.center = CGPointMake(center.x + translation.x, center.y + translation.y);
50 [recognizer setTranslation:CGPointZero inView:self.view];
51
52 if (recognizer.state == UIGestureRecognizerStateEnded) {
53 //计算速度向量的长度,当他小于200时,滑行会很短
54 CGPoint velocity = [recognizer velocityInView:self.view];
55 CGFloat magnitude = sqrtf((velocity.x * velocity.x) + (velocity.y * velocity.y));
56 CGFloat slideMult = magnitude / 200;
57 //NSLog(@"magnitude: %f, slideMult: %f", magnitude, slideMult); //e.g. 397.973175, slideMult: 1.989866
58
59 //基于速度和速度因素计算一个终点
60 float slideFactor = 0.1 * slideMult;
61 CGPoint finalPoint = CGPointMake(center.x + (velocity.x * slideFactor),
62 center.y + (velocity.y * slideFactor));
63 //限制小[cornerRadius]和大边界值[self.view.bounds.size.width - cornerRadius],以免拖动出屏幕界限
64 finalPoint.x = MIN(MAX(finalPoint.x, cornerRadius),
65 self.view.bounds.size.width - cornerRadius);
66 finalPoint.y = MIN(MAX(finalPoint.y, cornerRadius),
67 self.view.bounds.size.height - cornerRadius);
68
69 //使用 UIView 动画使 view 滑行到终点
70 [UIView animateWithDuration:slideFactor*2
71 delay:0
72 options:UIViewAnimationOptionCurveEaseOut
73 animations:^{
74 recognizer.view.center = finalPoint;
75 }
76 completion:nil];
77 }
78 }
79
80 /**
81 * 处理捏合手势
82 *
83 * @param recognizer 捏合手势识别器对象实例
84 */
85 - (void)handlePinch:(UIPinchGestureRecognizer *)recognizer {
86 CGFloat scale = recognizer.scale;
87 recognizer.view.transform = CGAffineTransformScale(recognizer.view.transform, scale, scale); //在已缩放大小基础下进行累加变化;区别于:使用 CGAffineTransformMakeScale 方法是在原大小基础下进行变化
88 recognizer.scale = 1.0;
89 }
90
91 /**
92 * 处理旋转手势
93 *
94 * @param recognizer 旋转手势识别器对象实例
95 */
96 - (void)handleRotation:(UIRotationGestureRecognizer *)recognizer {
97 recognizer.view.transform = CGAffineTransformRotate(recognizer.view.transform, recognizer.rotation);
98 recognizer.rotation = 0.0;
99 }
100
101 /**
102 * 处理点按手势
103 *
104 * @param recognizer 点按手势识别器对象实例
105 */
106 - (void)handleTap:(UITapGestureRecognizer *)recognizer {
107 UIView *view = recognizer.view;
108 view.transform = CGAffineTransformMakeScale(1.0, 1.0);
109 view.transform = CGAffineTransformMakeRotation(0.0);
110 view.alpha = 1.0;
111 }
112
113 /**
114 * 处理长按手势
115 *
116 * @param recognizer 点按手势识别器对象实例
117 */
118 - (void)handleLongPress:(UILongPressGestureRecognizer *)recognizer {
119 //长按的时候,设置不透明度为0.7
120 recognizer.view.alpha = 0.7;
121 }
122
123 /**
124 * 处理轻扫手势
125 *
126 * @param recognizer 轻扫手势识别器对象实例
127 */
128 - (void)handleSwipe:(UISwipeGestureRecognizer *)recognizer {
129 //代码块方式封装操作方法
130 void (^positionOperation)() = ^() {
131 CGPoint newPoint = recognizer.view.center;
132 newPoint.y -= 20.0;
133 _imgV.center = newPoint;
134
135 newPoint.y += 40.0;
136 _imgV2.center = newPoint;
137 };
138
139 //根据轻扫方向,进行不同控制
140 switch (recognizer.direction) {
141 case UISwipeGestureRecognizerDirectionRight: {
142 positionOperation();
143 break;
144 }
145 case UISwipeGestureRecognizerDirectionLeft: {
146 positionOperation();
147 break;
148 }
149 case UISwipeGestureRecognizerDirectionUp: {
150 break;
151 }
152 case UISwipeGestureRecognizerDirectionDown: {
153 break;
154 }
155 }
156 }
157
158 /**
159 * 处理自定义手势
160 *
161 * @param recognizer 自定义手势识别器对象实例
162 */
163 - (void)handleCustomGestureRecognizer:(KMGestureRecognizer *)recognizer {
164 //代码块方式封装操作方法
165 void (^positionOperation)() = ^() {
166 CGPoint newPoint = recognizer.view.center;
167 newPoint.x -= 20.0;
168 _imgV.center = newPoint;
169
170 newPoint.x += 40.0;
171 _imgV2.center = newPoint;
172 };
173
174 positionOperation();
175 }
176
177
178 #pragma mark - 绑定手势操作
179 /**
180 * 绑定拖动手势
181 *
182 * @param imgVCustom 绑定到图片视图对象实例
183 */
184 - (void)bindPan:(UIImageView *)imgVCustom {
185 UIPanGestureRecognizer *recognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self
186 action:@selector(handlePan:)];
187 [imgVCustom addGestureRecognizer:recognizer];
188 }
189
190 /**
191 * 绑定捏合手势
192 *
193 * @param imgVCustom 绑定到图片视图对象实例
194 */
195 - (void)bindPinch:(UIImageView *)imgVCustom {
196 UIPinchGestureRecognizer *recognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self
197 action:@selector(handlePinch:)];
198 [imgVCustom addGestureRecognizer:recognizer];
199 //[recognizer requireGestureRecognizerToFail:imgVCustom.gestureRecognizers.firstObject];
200 }
201
202 /**
203 * 绑定旋转手势
204 *
205 * @param imgVCustom 绑定到图片视图对象实例
206 */
207 - (void)bindRotation:(UIImageView *)imgVCustom {
208 UIRotationGestureRecognizer *recognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self
209 action:@selector(handleRotation:)];
210 [imgVCustom addGestureRecognizer:recognizer];
211 }
212
213 /**
214 * 绑定点按手势
215 *
216 * @param imgVCustom 绑定到图片视图对象实例
217 */
218 - (void)bindTap:(UIImageView *)imgVCustom {
219 UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self
220 action:@selector(handleTap:)];
221 //使用一根手指双击时,才触发点按手势识别器
222 recognizer.numberOfTapsRequired = 2;
223 recognizer.numberOfTouchesRequired = 1;
224 [imgVCustom addGestureRecognizer:recognizer];
225 }
226
227 /**
228 * 绑定长按手势
229 *
230 * @param imgVCustom 绑定到图片视图对象实例
231 */
232 - (void)bindLongPress:(UIImageView *)imgVCustom {
233 UILongPressGestureRecognizer *recognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)];
234 recognizer.minimumPressDuration = 0.5; //设置小长按时间;默认为0.5秒
235 [imgVCustom addGestureRecognizer:recognizer];
236 }
237
238 /**
239 * 绑定轻扫手势;支持四个方向的轻扫,但是不同的方向要分别定义轻扫手势
240 */
241 - (void)bindSwipe {
242 //向右轻扫手势
243 UISwipeGestureRecognizer *recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self
244 action:@selector(handleSwipe:)];
245 recognizer.direction = UISwipeGestureRecognizerDirectionRight; //设置轻扫方向;默认是 UISwipeGestureRecognizerDirectionRight,即向右轻扫
246 [self.view addGestureRecognizer:recognizer];
247 [recognizer requireGestureRecognizerToFail:_customGestureRecognizer]; //设置以自定义挠痒手势优先识别
248
249 //向左轻扫手势
250 recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self
251 action:@selector(handleSwipe:)];
252 recognizer.direction = UISwipeGestureRecognizerDirectionLeft;
253 [self.view addGestureRecognizer:recognizer];
254 [recognizer requireGestureRecognizerToFail:_customGestureRecognizer]; //设置以自定义挠痒手势优先识别
255 }
256
257 /**
258 * 绑定自定义挠痒手势;判断是否有三次不同方向的动作,如果有则手势结束,将执行回调方法
259 */
260 - (void)bingCustomGestureRecognizer {
261 //当 recognizer.state 为 UIGestureRecognizerStateEnded 时,才执行回调方法 handleCustomGestureRecognizer:
262
263 //_customGestureRecognizer = [KMGestureRecognizer new];
264 _customGestureRecognizer = [[KMGestureRecognizer alloc] initWithTarget:self
265 action:@selector(handleCustomGestureRecognizer:)];
266 [self.view addGestureRecognizer:_customGestureRecognizer];
267 }
268
269 - (void)layoutUI {
270 //图片视图 _imgV
271 UIImage *img = [UIImage imageNamed:@"Emoticon_tusiji_icon"];
272 CGFloat cornerRadius = img.size.width;
273 _imgV = [[UIImageView alloc] initWithImage:img];
274 _imgV.frame = CGRectMake(20.0, 20.0,
275 cornerRadius * 2, cornerRadius * 2);
276 _imgV.userInteractionEnabled = YES;
277 _imgV.layer.masksToBounds = YES;
278 _imgV.layer.cornerRadius = cornerRadius;
279 _imgV.layer.borderWidth = 2.0;
280 _imgV.layer.borderColor = [UIColor grayColor].CGColor;
281 [self.view addSubview:_imgV];
282
283 //图片视图 _imgV2
284 img = [UIImage imageNamed:@"Emoticon_tusiji_icon2"];
285 cornerRadius = img.size.width;
286 _imgV2 = [[UIImageView alloc] initWithImage:img];
287 _imgV2.frame = CGRectMake(20.0, 40.0 + _imgV.frame.size.height,
288 cornerRadius * 2, cornerRadius * 2);
289 _imgV2.userInteractionEnabled = YES;
290 _imgV2.layer.masksToBounds = YES;
291 _imgV2.layer.cornerRadius = cornerRadius;
292 _imgV2.layer.borderWidth = 2.0;
293 _imgV2.layer.borderColor = [UIColor orangeColor].CGColor;
294 [self.view addSubview:_imgV2];
295
296
297 [self bindPan:_imgV];
298 [self bindPinch:_imgV];
299 [self bindRotation:_imgV];
300 [self bindTap:_imgV];
301 [self bindLongPress:_imgV];
302
303 [self bindPan:_imgV2];
304 [self bindPinch:_imgV2];
305 [self bindRotation:_imgV2];
306 [self bindTap:_imgV2];
307 [self bindLongPress:_imgV2];
308
309 //为了处理手势识别优先级的问题,这里需先绑定自定义挠痒手势
310 [self bingCustomGestureRecognizer];
311 [self bindSwipe];
312 }
313
314 @end
本文内容不用于商业目的,如涉及知识产权问题,请权利人联系SPASVO小编(021-61079698-8054),我们将立即处理,马上删除。
相关推荐
了解ios自动化测试要知道哪些知识?IOS配置、远程设备管理和TestAgent的启动方式移动测试工具MR是如何进行IOS设备录制以及相关脚本命令编辑操作的?MobileRunner iOS部署文档iOS单元测试iOS遭攻击频率低于安卓 但后果更严重iOS的JSON解析及用例设计iOS UnitTest单元测试(iOS)一个让我找了6小时的BugAppium iOS 10 跑起来浅谈iOS单元测试iOS UI自动化测试初探iOS APP打包分发给远程的手机测试复杂业务场景下如何进行iOS端自动化测试微信关闭iOS版公众号打赏功能,只因苹果想分成?苹果企业账号遭滥用:iOS漏洞“留出”赌博应用通道
更新发布
功能测试和接口测试的区别
2023/3/23 14:23:39如何写好测试用例文档
2023/3/22 16:17:39常用的选择回归测试的方式有哪些?
2022/6/14 16:14:27测试流程中需要重点把关几个过程?
2021/10/18 15:37:44性能测试的七种方法
2021/9/17 15:19:29全链路压测优化思路
2021/9/14 15:42:25性能测试流程浅谈
2021/5/28 17:25:47常见的APP性能测试指标
2021/5/8 17:01:11热门文章
常见的移动App Bug??崩溃的测试用例设计如何用Jmeter做压力测试QC使用说明APP压力测试入门教程移动app测试中的主要问题jenkins+testng+ant+webdriver持续集成测试使用JMeter进行HTTP负载测试Selenium 2.0 WebDriver 使用指南