UIPanGestureRecognizer的使用
UIGestureRecognizer是一个定义基本手势的抽象类,具体什么手势,在以下子类中包含:
1、拍击UITapGestureRecognizer (任意次数的拍击)
2、向里或向外捏UIPinchGestureRecognizer (用于缩放)
3、摇动或者拖拽UIPanGestureRecognizer (拖动)
4、擦碰UISwipeGestureRecognizer (以任意方向)
5、旋转UIRotationGestureRecognizer (手指朝相反方向移动)
6、长按UILongPressGestureRecognizer (长按)
今天一同学问到UIPanGestureRecognizer类中translationInView方法和velocityInView方法有什么区别,因为我也好久没看IOS,一丢下就很难拾起,故今天研究下这个问题
UIPanGestureRecognizer主要用于拖动,比如桌面上有一张图片uiimageview,你想让它由原始位置拖到任何一个位置,就是图片跟着你的手指走动,那么就需要用到该类了。
以下代码表示给一个图片视图指定一个UIPanGestureRecognizer手势当该图片捕获到用户的拖动手势时会调用回调函数handlePan
- UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
- [self.imgView setUserInteractionEnabled:YES];
- [self.imgView addGestureRecognizer:pan];
- [pan release];
handlePan函数代码如下:
- - (void) handlePan: (UIPanGestureRecognizer *)rec{
- NSLog(@"xxoo---xxoo---xxoo");
- CGPoint point = [rec translationInView:self.view];
- NSLog(@"%f,%f",point.x,point.y);
- rec.view.center = CGPointMake(rec.view.center.x + point.x, rec.view.center.y + point.y);
- [rec setTranslation:CGPointMake(0, 0) inView:self.view];
- }
以下为本人自己的理解,有不到之处请看官务必指教12
- (CGPoint)translationInView:(UIView *)view方法的API解释如下:
The translation of the pan gesture in the coordinate system of the specified view.
Return Value
A point identifying the new location of a view in the coordinate system of its designated superview.
字面理解是:
在指定的视图坐标系统中转换(拖动?) pan gesture
返回参数:返回一个明确的新的坐标位置,在指定的父视图坐标系统中
简单的理解就是
该方法返回在横坐标上、纵坐标上拖动了多少像素
因为拖动起来一直是在递增,所以每次都要用setTranslation:方法制0这样才不至于不受控制般滑动出视图
- (CGPoint)velocityInView:(UIView *)view方法的API解释如下:
The velocity of the pan gesture in the coordinate system of the specified view.
Return Value
The velocity of the pan gesture, which is expressed in points per second. The velocity is broken into horizontal and vertical components.
字面理解:
在指定坐标系统中pan gesture拖动的速度
返回参数:返回这种速度
简单的理解就是
你拖动这个图片的时候肯定有个速度,因此返回值就是你拖动时X和Y轴上的速度,速度是矢量,有方向。
参考资料
http://www.cnblogs.com/andyque/archive/2011/12/30/2307060.html
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 拖动TableView改变其高度
需求:项目中要求tableView的高度随着手拖动的位置而改变如下图: 关键代码如下: - (void)viewDidLoad{ panGestureRecognizer = [[UIPanGestu ...
- UIPanGestureRecognizer判断滑动的方向
.h文件 CGFloat const gestureMinimumTranslation = 20.0 ; typedef enum : NSInteger { kCameraMoveDirectio ...
随机推荐
- 【原】日志处理-Spark
日志信息如下所示: 1.1.1.1 - - [21/Jul/2014:10:00:00 -0800] "GET /majihua/article/284234 HTTP/1.1" ...
- Java笔记(十二)……类中各部分加载顺序及存放位置问题
什么时候会加载类 使用到类中的内容时加载,三种情况: 创建对象:new StaticDemo(); 使用类中的静态成员:StaticCode.num = 9; StaticCode.getNum() ...
- 算法导论学习-binary search tree
1. 概念: Binary-search tree(BST)是一颗二叉树,每个树上的节点都有<=1个父亲节点,ROOT节点没有父亲节点.同时每个树上的节点都有[0,2]个孩子节点(left ch ...
- __attribute__ ((__section__ (".init.text")))
在kernel中有很多__init,这个东东到底是何方神圣捏?且听小生我一一道来.下面是其定义:file:/include/linux/init.h 43 #define __init __ ...
- Python队列服务 Python RQ Functions from the __main__ module cannot be processed by workers.
在使用Python队列服务 Python RQ 时候的报错: Functions from the __main__ module cannot be processed by workers. 原因 ...
- windows 下安装elasticsearch
1.下载 elasticsearch-2.3.3.zip 注意::elasticsearch 版本太高的话,java运行不起来 elasticsearch-1.5.2下载地址 http://downl ...
- linux命令之cat
转自:http://www.cnblogs.com/peida/archive/2012/10/30/2746968.html cat命令的用途是连接文件或标准输入并打印.这个命令常用来显示文件内容, ...
- 如何通过进程名获取进程ID
博客搬到了fresky.github.io - Dawei XU,请各位看官挪步.最新的一篇是:如何通过进程名获取进程ID.
- HTML几类标签的应用总结
打开DREAMWEAVER,新建HTML,如下图: body的属性: bgcolor 页面背景色 background 背景壁纸.图片 text 文字颜色 topmargin 上边距 leftm ...
- js返回上一页方法区别
history.back(-1):直接返回当前页的上一页,数据全部消息,是个新页面 history.go(-1):也是返回当前页的上一页,不过表单里的数据全部还在