zoukankan      html  css  js  c++  java
  • UIPanGestureRecognizer的使用

    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

  • 相关阅读:
    管理者如何保持团队稳定性
    信息系统项目管理师考试之案例分析答题技巧
    【信息系统项目管理师】信息系统项目管理师计算题汇总
    [转]信息系统项目管理师计算题汇总
    Oracle
    PLSQL中查询数据的时候查询结果显示中文乱码
    项目管理知识体系指南(PMBOK 指南)
    linux查看分区命令和根分区扩容方法
    docker启动命令,docker重启命令,docker关闭命令
    iOS与JavaScript交互三: MessageHandler--window.webkit.messageHandlers.<name>.postMessage(<messageBody>)
  • 原文地址:https://www.cnblogs.com/CodingMann/p/5292506.html
Copyright © 2011-2022 走看看