• UIPanGestureRecognizer学习笔记


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

    废话少说 开始笔记

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

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

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

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

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

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

     handlePan函数代码如下:

    C代码  收藏代码
    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

     zhuan:http://lijinfengjava.iteye.com/blog/1735986 博文很好,我得向他学习

     
  • 相关阅读:
    实验三 进程调度模拟程序
    实验二作业调度模拟程序
    最新广商小助手 项目进展 OpenGL ES 3D在我项目中引用 代码太多只好选重要部分出来
    最后冲刺 我的项目 广商小助手
    最新一课 老师指点用Listview适配器
    安卓小学生四则运算
    大三上学期安卓一边学一边开始做一个自己觉得可以的项目 广商小助手App 加油
    我要再接再力 学更多
    用场景来规划测试工作
    阅读第13,14,15,16,17章
  • 原文地址:https://www.cnblogs.com/AbelChen1991/p/3647026.html
Copyright © 2020-2023  润新知