http://blog.csdn.net/huifeidexin_1/article/details/8282035

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. uiscrollview上的 uipangesturerecognizer冲突

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

  7. UIPanGestureRecognizer的使用

    UIGestureRecognizer是一个定义基本手势的抽象类,具体什么手势,在以下子类中包含: 1.拍击UITapGestureRecognizer (任意次数的拍击)      2.向里或向外捏 ...

  8. UIPanGestureRecognizer 拖动TableView改变其高度

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

  9. UIPanGestureRecognizer判断滑动的方向

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

随机推荐

  1. 面试题_31_to_47_JVM 底层与GC(Garbage Collection)的面试问题

    31)64 位 JVM 中,int 的长度是多数?Java 中,int 类型变量的长度是一个固定值,与平台无关,都是 32 位.意思就是说,在 32 位 和 64 位 的Java 虚拟机中,int 类 ...

  2. perl基本语法

    标量 标量是 Perl 中最简单的数据类型.大多数的标量是数字(如 255 或 3.25e20)或者字符串(如 hello或者盖茨堡地址). 数字 perl中所有数字内部的格式都是双精度浮点数. 浮点 ...

  3. Codeforces Round #243 (Div. 2) C. Sereja and Swaps(优先队列 暴力)

    题目 题意:求任意连续序列的最大值,这个连续序列可以和其他的 值交换k次,求最大值 思路:暴力枚举所有的连续序列.没做对是因为 首先没有认真读题,没看清交换,然后,以为是dp或者贪心 用了一下贪心,各 ...

  4. bzoj1562

    很明显是二分图匹配,关键是怎么求字典序最小 想到两种做法,首先是直接匹配,然后从第一位贪心调整 第二种是从最后一个倒着匹配,每次匹配都尽量选小的,这样一定能保证字典序最小 type node=reco ...

  5. 解决编译报错:Unable to copy file, because it is being used by another process.

    Error    63    Unable to copy file "D:\DEV\XXX Website\trunk\4 Source Code\Common\WebControls\b ...

  6. UVA 11396 Claw Decomposition(二分图)

    以“爪”形为单元,问所给出的无向图中能否被完全分割成一个个单元. 分析图的性质,由于已知每个点的度是3,所以“爪”之间是相互交错的,即把一个“爪”分为中心点和边缘点,中心点被完全占据,而边缘点被三个“ ...

  7. I.MX6 Power off register hacking

    /*********************************************************************** * I.MX6 Power off register ...

  8. django - django 承接nginx请求

    # -*- coding: utf-8 -*- import os import sys import tornado.ioloop import tornado.web import tornado ...

  9. Windows 下音频数据采集和播放

    音频操作所需头文件和链接库 #include<mmsystem.h>#include<mmreg.h>#pragma  comment(lib, "winmm.lib ...

  10. liunx安装qq

    http://www.07net01.com/电脑玩物 http://www.07net01.com/2014/09/68186.html 安装qq 一开始,我在ubuntu14.04下安装的QQ版本 ...