手势操作知识大餐 ——iOS
来源:互联网 发布:不知道睡没睡着 知乎 编辑:程序博客网 时间:2024/05/23 19:18
1.点击(非连续型)
UITapGestureRecognizer类
Eg:
1)定义元素
UIView* view = [[UIView alloc]initWithFrame:CGRectMake(0, 100, 300, 200)];
view.backgroundColor = [UIColor yellowColor];
[self.view addSubview:view];
注意:当元素为UIImageView时,一定要设置它的userInteractionEnabled值为YES.
2)元素上添加手势
UITapGestureRecognizer* clickRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapGestureRecognizer:)];
clickRecognizer.numberOfTouchesRequired = 2;//1.一根手指头 2.两根手指头 。。。(指头个数)
clickRecognizer.numberOfTapsRequired = 2;//1.单击 2.双击(敲击次数)
// clickRecognizer.numberOfTouches = 1;//暂时未知功能
[view addGestureRecognizer:clickRecognizer];
2.拖动(连续型)
UIPanGestureRecognizer类
Eg:1)定义元素(同上)
2)元素上添加手势
UIPanGestureRecognizer *recognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
[imageView addGestureRecognizer:recognizer];
3)拖动方法
-(void)handlePan:(UIPanGestureRecognizer*)recognizer{//可以根据自己需求修改拖动元素时的算法
//视图前置操作
[recognizer.view.superview bringSubviewToFront:recognizer.view];
CGPoint center = recognizer.view.center;
CGFloat cornerRadius = recognizer.view.frame.size.width / 2;
CGPoint translation = [recognizer translationInView:self.view];
//NSLog(@"%@", NSStringFromCGPoint(translation));
recognizer.view.center = CGPointMake(center.x + translation.x, center.y + translation.y);
[recognizer setTranslation:CGPointZero inView:self.view];
if (recognizer.state == UIGestureRecognizerStateEnded) {
//计算速度向量的长度,当他小于200时,滑行会很短
CGPoint velocity = [recognizer velocityInView:self.view];
CGFloat magnitude = sqrtf((velocity.x * velocity.x) + (velocity.y * velocity.y));
CGFloat slideMult = magnitude / 200;
//NSLog(@"magnitude: %f, slideMult: %f", magnitude, slideMult); //e.g. 397.973175, slideMult: 1.989866
//基于速度和速度因素计算一个终点
float slideFactor = 0.1 * slideMult;
CGPoint finalPoint = CGPointMake(center.x + (velocity.x * slideFactor),
center.y + (velocity.y * slideFactor));
//限制最小[cornerRadius]和最大边界值[self.view.bounds.size.width - cornerRadius],以免拖动出屏幕界限
finalPoint.x = MIN(MAX(finalPoint.x, cornerRadius),
self.view.bounds.size.width - cornerRadius);
finalPoint.y = MIN(MAX(finalPoint.y, cornerRadius),
self.view.bounds.size.height - cornerRadius);
//使用 UIView 动画使 view 滑行到终点
[UIView animateWithDuration:slideFactor*2
delay:0
options:UIViewAnimationOptionCurveEaseOut
animations:^{
recognizer.view.center = finalPoint;
}
completion:nil];
}
NSLog(@"test move.");
}
待续。。。
- 手势操作知识大餐 ——iOS
- ios的手势操作—UIGestureRec…
- UI—— ios的手势操作之UIGestureRecognizer浅析
- IOS开发—手势操作(点击+滑动)
- IOS 之 手势相关知识
- iOS手势操作
- iOS手势操作
- iOS----手势操作
- ios手势操作
- UIGestureRecognizer 手势操作 iOS
- 手势操作 IOS
- ios 手势操作举例
- iOS手势操作UIGestureRecognizer
- iOS 手势操作
- iOS-自定义手势操作
- Apress水果大餐——移动开发
- 天下第一大餐—健康的营养早餐
- ios开发——手势
- 用Go开发WeChat接口的一个小坑
- MySQL 存储引擎
- POJ 2195 Going Home(费用流)
- 171. Excel Sheet Column Number
- 栈和队列相关问题
- 手势操作知识大餐 ——iOS
- 【codevs1540】银河英雄传说,以前屯着没做的并查集
- 217. Contains Duplicate
- ARP扫描攻击(JAVA实现)
- CodeForces 158B Taxi
- Linux学习笔记(5)----shell默认的版本bash及变量的设置
- std::lower_bound()和std::upper_bound()总结
- 学习记录 - 3
- poj_3974 Palindrome(最长回文子串)