(转)UIPanGestureRecognizer
UIPanGestureRecognizer是UIGestureRecognizer类的一个扩展类,其扩展类有UITapGestureRecognizer,UIPinchGestureRecognizer,UIRotationGestureRecognizer,UISwipeGestureRecognizer,UIPanGestureRecognizer,UILongPressGestureRecognizer。
借助这些类,可以实现UIView对象的一些操作如对象放大缩小,移动,旋转,滑动,轻击等。再也不用去重写UIView的touchBegin等方法来实现这些功能。
知识点:
UIGestureRecognizer是一个定义基本手势的抽象类,具体什么手势,在以下子类中包含:
1、拍击UITapGestureRecognizer (任意次数的拍击)
2、向里或向外捏UIPinchGestureRecognizer (用于缩放)
3、摇动或者拖拽UIPanGestureRecognizer (拖动)
4、擦碰UISwipeGestureRecognizer (以任意方向)
5、旋转UIRotationGestureRecognizer (手指朝相反方向移动)
6、长按UILongPressGestureRecognizer (长按)
这些操作的目的都是用来修改UIView对象的frame,center,bounds属性,还有一个Transform属性。
我写了一个例子,在UIView和UITableView上分别添加UIPanGestureRecognizer,实现两个对象在手指按住对象于屏幕中拖动的效果。
声明一个UIPanGestureRecognizer对象,添加到UIView对象上去。UIView类有这样的方法用来动态添加和删除UIPanGestureRecognizer对象。
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
[testPanView addGestureRecognizer:panRecognizer];
UIView管理手势识别器的方法有:
– addGestureRecognizer:
– removeGestureRecognizer:
gestureRecognizers property
– gestureRecognizerShouldBegin:
我在viewDidAppear:方法中,动态添加视图和手势识别器。然后,实现识别器需要操作的两个方法,用来移动视图对象。
在这两个方法中最终的方法是这个 CGPoint translatedPoint = [recognizer translationInView:self.view];
每一次拖动操作状态,都会获取到translatedPoint,从开始到结束。它是一个绝对值,可以看着在”self.view“对应的坐标体系中,拖动的视图对象center的移动开始和结束的点差。
最简单的处理过程是这样:
CGPoint translatedPoint = [recognizer translationInView:self.view];
CGFloat x = recognizer.view.center.x + translatedPoint.x;
CGFloat y = recognizer.view.center.y + translatedPoint.y;
recognizer.view.center = CGPointMake(x, y);
[recognizer setTranslation:CGPointMake(0, 0) inView:self.view];
首先获取到移动点的值,然后算一下视图的center值,相加一下,就得到在self.view坐标体系中,视图该移动到那个center上,一次结束就清零一次。
因为拖动操作持续进行,所以,这个过程会持续执行。
稍微复杂点的处理过程,会捕获到拖动开始,移动,结束等几个状态下的translatedPoint的值。然后做一下逻辑处理,如视图不能溢出self.view的坐标系中,如在结束时会根据方向自动滑动到某个位置。可以在handlePan2:方法中找到这些逻辑的实现代码。
- (void)viewDidAppear:(BOOL)animated
{
NSLog(@" viewDidAppear is at %@.", [NSDate date]);
UIImage *image = [UIImage imageNamed:@"5.jpg"];
testPanView = [[UIView alloc] initWithFrame:CGRectMake(18, 11, 100, 100)];
UIImageView *imageview = [[UIImageView alloc] initWithFrame:[testPanView frame]];
[imageview setImage:image];
[testPanView addSubview:imageview];
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
[panRecognizer setMinimumNumberOfTouches:1];
[panRecognizer setMaximumNumberOfTouches:1];
[panRecognizer setDelegate:self];
[testPanView addGestureRecognizer:panRecognizer];
[self.view addSubview:testPanView];
testPanTableView = [[UITableView alloc] initWithFrame:CGRectMake(118, 121, 100, 100) style:UITableViewStylePlain];
UIPanGestureRecognizer *panRecognizer2 = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan2:)];
[panRecognizer2 setMinimumNumberOfTouches:1];
[panRecognizer2 setMaximumNumberOfTouches:1];
[panRecognizer2 setDelegate:self];
[testPanTableView addGestureRecognizer:panRecognizer2];
[self.view addSubview:testPanTableView];
}
- (void)handlePan:(UIPanGestureRecognizer *)recognizer
{
CGPoint translatedPoint = [recognizer translationInView:self.view];
NSLog(@"gesture translatedPoint is %@", NSStringFromCGPoint(translatedPoint));
CGFloat x = recognizer.view.center.x + translatedPoint.x;
CGFloat y = recognizer.view.center.y + translatedPoint.y;
recognizer.view.center = CGPointMake(x, y);
NSLog(@"pan gesture testPanView moving is %@,%@", NSStringFromCGPoint(recognizer.view.center), NSStringFromCGRect(recognizer.view.frame));
[recognizer setTranslation:CGPointMake(0, 0) inView:self.view];
}
- (void)handlePan2:(UIPanGestureRecognizer *)recognizer
{
// NSLog(@"gesture translatedPoint xxoo xxoo");
CGPoint translatedPoint = [recognizer translationInView:self.view];
if ([(UIPanGestureRecognizer *)recognizer state] == UIGestureRecognizerStateBegan) {
firstX = recognizer.view.center.x;
firstY = recognizer.view.center.y;
NSLog(@"self.view bounds is %@", NSStringFromCGRect(self.view.bounds));
NSLog(@"pan gesture testPanView begin is %@,%@", NSStringFromCGPoint([recognizer view].center), NSStringFromCGRect([recognizer view].frame));
}
if ([(UIPanGestureRecognizer *)recognizer state] == UIGestureRecognizerStateChanged) {
CGFloat x = firstX + translatedPoint.x;
CGFloat y = firstX + translatedPoint.y;
if (x < recognizer.view.width / 2.0) {
x = recognizer.view.width / 2.0;
} else if (x + recognizer.view.width / 2.0 > self.view.width) {
x = self.view.width - recognizer.view.width / 2.0;
}
if (y < recognizer.view.height / 2.0) {
y = recognizer.view.height / 2.0;
} else if (y + recognizer.view.height / 2.0 > self.view.height) {
y = self.view.height - recognizer.view.height / 2.0;
}
NSLog(@"gesture translatedPoint moving is %@", NSStringFromCGPoint(translatedPoint));
recognizer.view.center = CGPointMake(x, y);
}
if (([(UIPanGestureRecognizer *)recognizer state] == UIGestureRecognizerStateEnded) || ([(UIPanGestureRecognizer *)recognizer state] == UIGestureRecognizerStateCancelled)) {
CGFloat x = recognizer.view.center.x;
CGFloat y = recognizer.view.center.y;
if (x > firstX) {
x = self.view.width - recognizer.view.width / 2.0;
} else {
x = recognizer.view.width / 2.0;
}
if (y > firstY) {
y = self.view.height - recognizer.view.height / 2.0;
} else {
y = recognizer.view.height / 2.0;
}
CGFloat velocityX = (0.2 *[recognizer velocityInView:self.view].x);
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:ABS(velocityX * 0.00002 + 0.2)];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
recognizer.view.center = CGPointMake(x, y);
[UIView commitAnimations];
NSLog(@"gesture translatedPoint end is %@", NSStringFromCGPoint(translatedPoint));
NSLog(@"pan gesture testPanView end is %@,%@", NSStringFromCGPoint([recognizer view].center), NSStringFromCGRect([recognizer view].frame));
}
}
(转)UIPanGestureRecognizer的更多相关文章
- 拖拽手势和清扫手势冲突时(UIPanGestureRecognizer和UISwipeGestureRecognizer冲突时)
故事发生在这样的情境上:给整个控制器添加了一个拖拽手势,然后又在控制上的每个Cell上加了左滑清扫手势,然后问题来了:只有拖拽手势起作用,而左滑手势没有效果了,然后怎么解决这个问题呢!先上图: 当给整 ...
- 如何判断UIPanGestureRecognizer的拖动方向
最近做一个项目,需要用到UIPanGestureRecognizer做一个侧滑菜单,需求是不能向右侧拖动(点击按钮右滑),但可以向左侧手势拖动收回:于是需要判断拖动的方向,百度了一下,网上大部分的答案 ...
- IOS开发之进阶篇第一章 - 姿势识别器UIPanGestureRecognizer
今天讲一下姿势识别器,UIGestureRecognizer这个是抽象类 1.拍击UITapGestureRecognizer (任意次数的拍击) 2.向里或向外捏UIPinchGestureReco ...
- iOS开发 UIPanGestureRecognizer手势抽象类
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@sel ...
- UIPanGestureRecognizer中translationInView的理解
原因是在破船大牛的blog上面看到了一个demo #import <UIKit/UIKit.h> @interface ViewController : UIViewController ...
- UIPanGestureRecognizer
http://blog.csdn.net/huifeidexin_1/article/details/8282035 UIGestureRecognizer是一个定义基本手势的抽象类,具体什么手势,在 ...
- uiscrollview上的 uipangesturerecognizer冲突
最近在tableview里的cell imageview加了个 uipangesturerecognizer发现优先滚动imageview,往上拖的时候,tableView不响应滚动了,原来是tabl ...
- UIPanGestureRecognizer的使用
UIGestureRecognizer是一个定义基本手势的抽象类,具体什么手势,在以下子类中包含: 1.拍击UITapGestureRecognizer (任意次数的拍击) 2.向里或向外捏 ...
- UIPanGestureRecognizer 拖动TableView改变其高度
需求:项目中要求tableView的高度随着手拖动的位置而改变如下图: 关键代码如下: - (void)viewDidLoad{ panGestureRecognizer = [[UIPanGestu ...
- UIPanGestureRecognizer判断滑动的方向
.h文件 CGFloat const gestureMinimumTranslation = 20.0 ; typedef enum : NSInteger { kCameraMoveDirectio ...
随机推荐
- 使用tmodjs
1.安装 npm install -g tmodjs 2.配置 我的模板都放在tpl文件夹中,htmls用于存放模板页面,每一个后缀名都是.html,而build用于存放编译后输出的模板js. 如果不 ...
- Java——HashSet和TreeSet的区别
HashSetHashSet有以下特点 不能保证元素的排列顺序,顺序有可能发生变化 不是同步的 集合元素可以是null,但只能放入一个null当向HashSet集合中存入一个元素时,HashSe ...
- Android5.0以上版本录屏实现
我录屏的方式是分别录制音频和视频,最后合并成mp4格式,比较麻烦,因为网上完整的教程比较少,所以我打算写一个完整版的,照着我的代码写完之后,至少是能够实现功能的,而不是简单的介绍下用法. 1既然是录制 ...
- ssh框架出现Java.lang.NoSuchMethodError: antlr.collections.AST.getLine()I错误
原因:因为Struts自带的antlr-2.7.2.jar,比Hibernate自带的antlr-2.7.7.jar的版本要低,存在jar包冲突现象,因此要删除前一个低版本的. 由于myeclipse ...
- vi 搜索
/ 向下搜索 ? 向上搜索 n 重复前一个搜索 N 反向重复前一个搜索
- JS 字符串 时间 数字函数操作 事件
字符串 操作 var s="abcdefg" s.tolowerCase() 转小写 s.toupperCase() 转大写 s.substring(2,5) 索引下 ...
- openstack No valid host was found. There are not enough hosts available.
root@dell-PowerEdge-T30:~# gedit /var/log/nova/nova-conductor.logroot@dell-PowerEdge-T30:~# gedit /v ...
- Words Prefixed Trans-
transit v. Pass across or through (an area) The new large ships will be too big to transit the Panam ...
- Codeforces Round #290 (Div. 2) _B找矩形环的三种写法
http://codeforces.com/contest/510/status/B 题目大意 给一个n*m 找有没有相同字母连起来的矩形串 第一种并查集 瞎搞一下 第一次的时候把val开成字符串了 ...
- git快速入门(MAC系统,github,ssh key)
如果使用过svn的话,git大致可以认为是多了本地库的svn.git先本地提交commit到本地库,然后再push到远程服务器的库.git是分布式的代码管理工具,基于SSH协议.ssh的作用就是为了不 ...