`

UIPanGestureRecognizer学习笔记

 
阅读更多

好久没来写笔记了,原因很简单,最近一直坐java网站,没顾上学IOS(偷懒没学..)

废话少说 开始笔记

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

分享到:
评论
1 楼 jimode2013 2013-06-18  
thanks

相关推荐

Global site tag (gtag.js) - Google Analytics