iOS各种手势

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

    1、拍击UITapGestureRecognizer (任意次数的拍击)  
    2、向里或向外捏UIPinchGestureRecognizer (用于缩放)  
    3、摇动或者拖拽UIPanGestureRecognizer (拖动) 
    4、擦碰UISwipeGestureRecognizer (以任意方向拖动,具体向哪个方向,需要自己手动设置。当需要多个手势方向时,需要创建多个swipeGestureRecongnizer对象)  
    5、旋转UIRotationGestureRecognizer (手指朝相反方向移动)  

    6、长按UILongPressGestureRecognizer (长按)

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. }  

UIPanGestureRecognizer类中translationInView方法和velocityInView方法有区别

apple官网解释:

- (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.


- (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.

这个返回的velocity 是矢量的 ,有大小有方向(velocity英文意思 就是 矢量速度 的意思)

猜你喜欢

转载自blog.csdn.net/junlaiyan/article/details/42143359