UIGestureRecognizer是一个定义基本手势的抽象类,具体什么手势,在以下子类中包含:

1、拍击UITapGestureRecognizer (任意次数的拍击)  
    2、向里或向外捏UIPinchGestureRecognizer (用于缩放)  
    3、摇动或者拖拽UIPanGestureRecognizer (拖动) 
    4、擦碰UISwipeGestureRecognizer (以任意方向)  
    5、旋转UIRotationGestureRecognizer (手指朝相反方向移动)  
    6、长按UILongPressGestureRecognizer (长按)

今天一同学问到UIPanGestureRecognizer类中translationInView方法和velocityInView方法有什么区别,因为我也好久没看IOS,一丢下就很难拾起,故今天研究下这个问题

UIPanGestureRecognizer主要用于拖动,比如桌面上有一张图片uiimageview,你想让它由原始位置拖到任何一个位置,就是图片跟着你的手指走动,那么就需要用到该类了。

以下代码表示给一个图片视图指定一个UIPanGestureRecognizer手势当该图片捕获到用户的拖动手势时会调用回调函数handlePan

  1. UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
  2. [self.imgView setUserInteractionEnabled:YES];
  3. [self.imgView addGestureRecognizer:pan];
  4. [pan release];

handlePan函数代码如下:

  1. - (void) handlePan: (UIPanGestureRecognizer *)rec{
  2. NSLog(@"xxoo---xxoo---xxoo");
  3. CGPoint point = [rec translationInView:self.view];
  4. NSLog(@"%f,%f",point.x,point.y);
  5. rec.view.center = CGPointMake(rec.view.center.x + point.x, rec.view.center.y + point.y);
  6. [rec setTranslation:CGPointMake(0, 0) inView:self.view];
  7. }

以下为本人自己的理解,有不到之处请看官务必指教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的使用的更多相关文章

  1. 拖拽手势和清扫手势冲突时(UIPanGestureRecognizer和UISwipeGestureRecognizer冲突时)

    故事发生在这样的情境上:给整个控制器添加了一个拖拽手势,然后又在控制上的每个Cell上加了左滑清扫手势,然后问题来了:只有拖拽手势起作用,而左滑手势没有效果了,然后怎么解决这个问题呢!先上图: 当给整 ...

  2. 如何判断UIPanGestureRecognizer的拖动方向

    最近做一个项目,需要用到UIPanGestureRecognizer做一个侧滑菜单,需求是不能向右侧拖动(点击按钮右滑),但可以向左侧手势拖动收回:于是需要判断拖动的方向,百度了一下,网上大部分的答案 ...

  3. IOS开发之进阶篇第一章 - 姿势识别器UIPanGestureRecognizer

    今天讲一下姿势识别器,UIGestureRecognizer这个是抽象类 1.拍击UITapGestureRecognizer (任意次数的拍击) 2.向里或向外捏UIPinchGestureReco ...

  4. iOS开发 UIPanGestureRecognizer手势抽象类

    UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@sel ...

  5. UIPanGestureRecognizer中translationInView的理解

    原因是在破船大牛的blog上面看到了一个demo #import <UIKit/UIKit.h> @interface ViewController : UIViewController ...

  6. UIPanGestureRecognizer

    http://blog.csdn.net/huifeidexin_1/article/details/8282035 UIGestureRecognizer是一个定义基本手势的抽象类,具体什么手势,在 ...

  7. uiscrollview上的 uipangesturerecognizer冲突

    最近在tableview里的cell imageview加了个 uipangesturerecognizer发现优先滚动imageview,往上拖的时候,tableView不响应滚动了,原来是tabl ...

  8. UIPanGestureRecognizer 拖动TableView改变其高度

    需求:项目中要求tableView的高度随着手拖动的位置而改变如下图: 关键代码如下: - (void)viewDidLoad{ panGestureRecognizer = [[UIPanGestu ...

  9. UIPanGestureRecognizer判断滑动的方向

    .h文件 CGFloat const gestureMinimumTranslation = 20.0 ; typedef enum : NSInteger { kCameraMoveDirectio ...

随机推荐

  1. Linux下简单的socket通信实例

    Linux下简单的socket通信实例 If you spend too much time thinking about a thing, you’ll never get it done. —Br ...

  2. Import the Add Email and Post Configuration to the SiteMap managed solution -Dynamices CRM

    We have prepared a managed solution named Add Email and Post Configuration to SiteMap that you can i ...

  3. wave文件(*.wav)格式、PCM数据格式, goldwave 可以播放pcm raw audio

    1. 音频简介 经常见到这样的描述: 44100HZ 16bit stereo 或者 22050HZ 8bit mono 等等. 44100HZ 16bit stereo: 每秒钟有 44100 次采 ...

  4. Linux的五个查找命令:find,locate,whereis,which,type

    使用电脑的时候,经常需要查找文件. 在Linux中,有很多方法可以做到这一点.国外网站LinuxHaxor总结了五条命令,你可以看看自己知道几条.大多数程序员,可能经常使用其中的2到3条,对这5条命令 ...

  5. 开发日志_Jan.8.2017

    这两天继续着手开发碰撞部分. 主要工作是写碰撞类和运动线程类.碰撞主要在于算法,运动线程只要管理好就行了. 之前碰撞测试中(即还未添加完整碰撞算法时)遇到各种bug,疑似机器人和小球的定位点不明所造成 ...

  6. 解决Debian-7.1下Chrome浏览器字体难看的问题

    首先在 Advance Setting 的 font 标签页下做如下配置: 然后在用户目录下创建 .fonts.conf 文件,内容如下: <?xml version='1.0'?> &l ...

  7. 判断richtextbox选中的是否为图片

    ) { Text = "Img"; } else { Text = "Form1"; }

  8. 初学ios遇到问题记录01

    刚刚接触IOS,花了一段时间看我基础部分的OC后 就想试着弄个小程序,于是看到 http://www.cnblogs.com/LooDo/p/3907064.html博文中的小程序,博主分析的很详细, ...

  9. 《编程导论(Java)&#183;2.1.3改写(override)》

    <编程导论(Java)·2.1.3改写(override)>,收集override内容. 方法改写(method overriding)是指对于父类定义的一个实例方法,同意子类提供自己的实 ...

  10. Kivy A to Z -- 怎样从python代码中直接訪问Android的Service

    在Kivy中,通过pyjnius扩展能够间接调用Java代码,而pyjnius利用的是Java的反射机制.可是在Python对象和Java对象中转来转去总让人感觉到十分别扭.好在android提供了b ...