码迷,mamicode.com
首页 > 其他好文 > 详细

手势知多少

时间:2015-06-25 19:09:28      阅读:251      评论:0      收藏:0      [点我收藏+]

标签:

UIGestureRecognizer 是一个具体手势的基类,提供了较为简单的手势实现方式  

The concrete subclasses of UIGestureRecognizer are the following:

一个gesture recognizer是针对一个特定的view的(包含其subview),用UIView的方法addGestureRecognize:去关联一个view

 

一个gesture recognizer是不参与UIView的事件响应链的

 

 

各个手势使用时的代码:

UITapGestureRecognizer

 

[cpp] view plaincopy
 
  1. - (void)viewDidLoad  
  2. {  
  3.     [super viewDidLoad];  
  4.     // Do any additional setup after loading the view from its nib.  
  5.       
  6.     UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)];  
  7.     if (![tapGesture respondsToSelector:@selector(locationInView:)]) {  
  8.         [tapGesture release];  
  9.         tapGesture = nil;  
  10.     }else {  
  11.         tapGesture.delegate = self;  
  12.         tapGesture.numberOfTapsRequired = 1; // The default value is 1.  
  13.         tapGesture.numberOfTouchesRequired = 1; // The default value is 1.  
  14.         [self.view addGestureRecognizer:tapGesture];  
  15.     }  
  16. }  

 

[cpp] view plaincopy
 
  1. - (void)handleGesture:(UIGestureRecognizer *)gestureRecognizer  
  2. {  
  3.     UIView *view = [gestureRecognizer view]; // 这个view是手势所属的view,也就是增加手势的那个view  
  4.       
  5.     switch (gestureRecognizer.state) {  
  6.         case UIGestureRecognizerStateEnded:{ // UIGestureRecognizerStateRecognized = UIGestureRecognizerStateEnded // 正常情况下只响应这个消息  
  7.             NSLog(@"======UIGestureRecognizerStateEnded || UIGestureRecognizerStateRecognized");  
  8.             break;  
  9.         }  
  10.         case UIGestureRecognizerStateFailed:{ //   
  11.             NSLog(@"======UIGestureRecognizerStateFailed");  
  12.             break;  
  13.         }  
  14.         case UIGestureRecognizerStatePossible:{ //   
  15.             NSLog(@"======UIGestureRecognizerStatePossible");  
  16.             break;  
  17.         }  
  18.         default:{  
  19.             NSLog(@"======Unknow gestureRecognizer");  
  20.             break;  
  21.         }  
  22.     }    
  23. }  
  24.   
  25. // 询问一个手势接收者是否应该开始解释执行一个触摸接收事件  
  26. - (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{  
  27. //    CGPoint currentPoint = [gestureRecognizer locationInView:self.view];  
  28. //    if (CGRectContainsPoint(CGRectMake(0, 0, 100, 100), currentPoint) ) {  
  29. //        return YES;  
  30. //    }  
  31. //      
  32. //    return NO;  
  33.       
  34.     return YES;  
  35. }  
  36.   
  37. // 询问delegate,两个手势是否同时接收消息,返回YES同事接收。返回NO,不同是接收(如果另外一个手势返回YES,则并不能保证不同时接收消息)the default implementation returns NO。  
  38. // 这个函数一般在一个手势接收者要阻止另外一个手势接收自己的消息的时候调用  
  39. - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{  
  40.     return NO;  
  41. }  
  42.   
  43. // 询问delegate是否允许手势接收者接收一个touch对象  
  44. // 返回YES,则允许对这个touch对象审核,NO,则不允许。  
  45. // 这个方法在touchesBegan:withEvent:之前调用,为一个新的touch对象进行调用  
  46. - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{  
  47.     return YES;  
  48. }  

 

 

UIPinchGestureRecognizer

 

[cpp] view plaincopy
 
  1. - (void)viewDidLoad  
  2. {  
  3.     [super viewDidLoad];  
  4.     // Do any additional setup after loading the view from its nib.  
  5.       
  6.     UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)];  
  7.     if (![pinchGesture respondsToSelector:@selector(locationInView:)]) {  
  8.         [pinchGesture release];  
  9.         pinchGesture = nil;  
  10.     }else {  
  11.         pinchGesture.delegate = self;  
  12.         [self.view addGestureRecognizer: pinchGesture];  
  13.     }    
  14. }  
  15.   
  16.   
  17. - (void)handleGesture:(UIPinchGestureRecognizer *)gestureRecognizer  
  18. {  
  19.       
  20.     UIView *view = [gestureRecognizer view]; // 这个view是手势所属的view,也就是增加手势的那个view  
  21.       
  22.       
  23.     /* 
  24.      scale属性: 可以理解为两手指之间的距离,其实是个比例,相对距离,不是绝对距离 
  25.                 以刚开始的两个手指对应的两个point的之间的距离为标准,此时scale=1. 
  26.                 若两手指之间距离减小,则scale不断变小,当两指重合,则变为0 
  27.                 若两手指之间距离变大,则scale不断增大,没有上限,看屏幕多大 
  28.       
  29.      velocity属性: 可以理解为两手指之间的移动速度,其实是个速度比例,相对速度,不是绝对速度 
  30.                 以刚开始的两个手指对应的两个point的之间的距离为标准,此时velocity=0. 
  31.                 若两手指之间距离减小,则velocity为负数,从-0开始,随着手指向里捏合的速度越快,负值越大,没有上限,我测试了下,可以到-20.009099,甚至更大 
  32.                 若两手指之间距离变大,则velocity不断正数,没有上限,从0开始,随着手指向外捏合的速度越快,值越大,没有上限,我测试了下,可以到170.234663,甚至更大 
  33.                 注意:在这个过程中,出现了nan值。  
  34.                      有关NAN值的介绍如下:(http://www.cnblogs.com/konlil/archive/2011/07/06/2099646.html) 
  35.       
  36.      浮点异常值:NAN,QNAN,SNAN 
  37.       
  38.      32位浮点数在机器中的表示按照IEEE的标准是这样的: 
  39.      +------+----------------+-------------------------------+    
  40.      | 1bit    |   8bit               |         23bit                            |    
  41.      +------+----------------+-------------------------------+ 
  42.      其中:1bit表示符号位(0表示正,1表示负),8bit表示指数(0~255,实际指数取值还要减去127,即指数取值区间为-127~128),23bit表示尾数。 
  43.      这里所要说的浮点异常值就是这种表示产生的几种特殊值,IEEE规定根据指数和尾数的不同分别可表示如下几种特殊值: 
  44.      1. 零值:按上述的浮点表述形式如果指数部分全部为0,并且尾数全部为0,则表示为浮点0.0,并且规定-0 = +0 
  45.      2. 非规格化值:如果指数全部为0,尾数非0,则表示非规格化的值,16进制看到的就是[80xxxxxx]h或者[00xxxxxx]h 
  46.      3. 无穷值:如果指数全部为1,尾数全部为0,则根据符号位分别表示正无穷大和负无穷大,16进制看到的就是[FF800000]h或者[7F800000]h 
  47.      4. NAN:主角来了,如果指数全部为1,尾数非0,则表示这个值不是一个真正的值(Not A Number)。NAN又分成两类:QNAN(Quiet NAN)和SNAN(Singaling NAN)。QNAN与SNAN的不同之处在于,QNAN的尾数部分最高位定义为1,SNAN最高位定义为0;QNAN一般表示未定义的算术运算结果,最常见的莫过于除0运算;SNAN一般被用于标记未初始化的值,以此来捕获异常。 
  48.      那么既然NAN不是一个真实的数值,在程序如何判断变量是否变成了NAN呢?大部分语言中针对NAN值都有一系列的函数定义,C语言中最常见的三个函数: 
  49.      _isnan(double x);                  //判断是否为NAN 
  50.      _finite(double x);                  //判读是否为无穷大 
  51.      _fpclass(double x);                //返回一系列的定义值,如:_FPCLASS_QNAN, _FPCLASS_SNAN,具体参考MSDN 
  52.  
  53.      */  
  54.     CGFloat scale = gestureRecognizer.scale;  
  55.     NSLog(@"======scale: %f", scale);  
  56.       
  57.     CGFloat velocity = gestureRecognizer.velocity;  
  58.     NSLog(@"======scvelocityale: %f", velocity);  
  59.       
  60.     /* 
  61.      捏合手势 
  62.       
  63.      这个一般情况下只响应 
  64.      UIGestureRecognizerStateBegan、 
  65.      UIGestureRecognizerStateEnded || UIGestureRecognizerStateRecognized、 
  66.      UIGestureRecognizerStateChanged消息, 
  67.      一个UIGestureRecognizerStateBegan,接下去是N多的UIGestureRecognizerStateChanged,scale的值此时会不断的变化,当手指离开时,响应UIGestureRecognizerStateEnded || UIGestureRecognizerStateRecognized 
  68.      */  
  69.     switch (gestureRecognizer.state) {  
  70.         case UIGestureRecognizerStateEnded:{ // UIGestureRecognizerStateRecognized = UIGestureRecognizerStateEnded  
  71.             NSLog(@"======UIGestureRecognizerStateEnded || UIGestureRecognizerStateRecognized");  
  72.             break;  
  73.         }  
  74.         case UIGestureRecognizerStateBegan:{ //   
  75.             NSLog(@"======UIGestureRecognizerStateBegan");  
  76.             break;  
  77.         }  
  78.         case UIGestureRecognizerStateChanged:{ //   
  79.             NSLog(@"======UIGestureRecognizerStateChanged");  
  80.               
  81.             gestureRecognizer.view.transform = CGAffineTransformScale(gestureRecognizer.view.transform, gestureRecognizer.scale, gestureRecognizer.scale);  
  82.             gestureRecognizer.scale = 1; // 重置,很重要!!!  
  83.               
  84.             break;  
  85.         }  
  86.         case UIGestureRecognizerStateCancelled:{ //   
  87.             NSLog(@"======UIGestureRecognizerStateCancelled");  
  88.             break;  
  89.         }  
  90.         case UIGestureRecognizerStateFailed:{ //   
  91.             NSLog(@"======UIGestureRecognizerStateFailed");  
  92.             break;  
  93.         }  
  94.         case UIGestureRecognizerStatePossible:{ //   
  95.             NSLog(@"======UIGestureRecognizerStatePossible");  
  96.             break;  
  97.         }  
  98.         default:{  
  99.             NSLog(@"======Unknow gestureRecognizer");  
  100.             break;  
  101.         }  
  102.     }    
  103. }  

 

 

UIRotationGestureRecognizer

 

[cpp] view plaincopy
 
  1. - (void)viewDidLoad  
  2. {  
  3.     [super viewDidLoad];  
  4.     // Do any additional setup after loading the view from its nib.  
  5.     UIRotationGestureRecognizer *rotationGesture = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)];  
  6.     if (![rotationGesture respondsToSelector:@selector(locationInView:)]) {  
  7.         [rotationGesture release];  
  8.         rotationGesture = nil;  
  9.     }else {  
  10.         rotationGesture.delegate = self;  
  11.           
  12.         [self.view addGestureRecognizer:rotationGesture];  
  13.     }  
  14. }  
  15.   
  16.   
  17. - (void)handleGesture:(UIRotationGestureRecognizer *)gestureRecognizer  
  18. {  
  19.     UIView *view = [gestureRecognizer view]; // 这个view是手势所属的view,也就是增加手势的那个view  
  20.       
  21.       
  22.     /* 
  23.      rotation属性: 可以理解为两手指之间的旋转的角度,其实是个比例,相对角度,不是绝对角度 
  24.      以刚开始的两个手指对应的两个point的之间的那条直线为标准,此时rotation=1. 
  25.      向顺时针旋转,则rotation为正数且不断变大,当旋转360度时,rotation大概为6左右,如果继续顺时针旋转,则角度会不断增加,两圈为12左右,此时若逆时针旋转,角度则不断变小 
  26.      向逆时针旋转,则rotation为负数且不断变小,当旋转360度时,rotation大概为-6左右 
  27.       
  28.      velocity属性: 可以理解为两手指之间的移动速度,其实是个速度比例,相对速度,不是绝对速度 
  29.      以刚开始的两个手指对应的两个point的之间的距离为标准,此时velocity=0. 
  30.      若两手指向顺时针旋转,则velocity为正数,从0开始,随着手指向里捏合的速度越快,值越大,没有上限 
  31.      若两手指向逆时针旋转,则velocity为负数数,没有上限,从-0开始,随着手指向外捏合的速度越快,值越小,没有上限 
  32.      */  
  33.     CGFloat rotation = gestureRecognizer.rotation;  
  34.     NSLog(@"===rotation: %f", rotation);  
  35.       
  36.     CGFloat velocity = gestureRecognizer.velocity;  
  37.     NSLog(@"======velocity: %f", velocity);  
  38.       
  39.       
  40.       
  41.     /* 
  42.      旋转手势 
  43.       
  44.      这个一般情况下只响应 
  45.      UIGestureRecognizerStateBegan、 
  46.      UIGestureRecognizerStateEnded || UIGestureRecognizerStateRecognized、 
  47.      UIGestureRecognizerStateChanged消息, 
  48.      一个UIGestureRecognizerStateBegan,接下去是N多的UIGestureRecognizerStateChanged,scale的值此时会不断的变化,当手指离开时,响应UIGestureRecognizerStateEnded || UIGestureRecognizerStateRecognized 
  49.      */  
  50.     switch (gestureRecognizer.state) {  
  51.         case UIGestureRecognizerStateEnded:{ // UIGestureRecognizerStateRecognized = UIGestureRecognizerStateEnded  
  52.             NSLog(@"======UIGestureRecognizerStateEnded || UIGestureRecognizerStateRecognized");  
  53.             break;  
  54.         }  
  55.         case UIGestureRecognizerStateBegan:{ //   
  56.             NSLog(@"======UIGestureRecognizerStateBegan");  
  57.             break;  
  58.         }  
  59.         case UIGestureRecognizerStateChanged:{ //   
  60.             NSLog(@"======UIGestureRecognizerStateChanged");  
  61.               
  62.             gestureRecognizer.view.transform = CGAffineTransformRotate(gestureRecognizer.view.transform, gestureRecognizer.rotation);  
  63.             gestureRecognizer.rotation = 0; // 重置 这个相当重要!!!   
  64.               
  65.             break;  
  66.         }  
  67.         case UIGestureRecognizerStateCancelled:{ //   
  68.             NSLog(@"======UIGestureRecognizerStateCancelled");  
  69.             break;  
  70.         }  
  71.         case UIGestureRecognizerStateFailed:{ //   
  72.             NSLog(@"======UIGestureRecognizerStateFailed");  
  73.             break;  
  74.         }  
  75.         case UIGestureRecognizerStatePossible:{ //   
  76.             NSLog(@"======UIGestureRecognizerStatePossible");  
  77.             break;  
  78.         }  
  79.         default:{  
  80.             NSLog(@"======Unknow gestureRecognizer");  
  81.             break;  
  82.         }  
  83.     }    
  84. }  

 

UISwipeGestureRecognizer

 

[cpp] view plaincopy
 
  1. - (void)viewDidLoad  
  2. {  
  3.     [super viewDidLoad];  
  4.     // Do any additional setup after loading the view from its nib.  
  5.       
  6.     /* 
  7.      同一个手势只能指定一个方向,不能同时指定多个方向,要指定多个方向 必须用多个手势 
  8.      */  
  9.       
  10.     // right  
  11.     UISwipeGestureRecognizer *swipeGestureRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)];  
  12.     if (![swipeGestureRight respondsToSelector:@selector(locationInView:)]) {  
  13.         [swipeGestureRight release];  
  14.         swipeGestureRight = nil;  
  15.     }else {  
  16.         swipeGestureRight.delegate = self;  
  17.         swipeGestureRight.numberOfTouchesRequired = 1;// 手指个数 The default value is 1.  
  18.         swipeGestureRight.direction = UISwipeGestureRecognizerDirectionRight;// 同一个手势只能指定一个方向,不能同时指定多个方向,要指定多个方向 必须用多个手势  
  19.         [self.view addGestureRecognizer:swipeGestureRight];  
  20.     }  
  21.       
  22.     // left  
  23.     UISwipeGestureRecognizer *swipeGestureLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)];  
  24.     if (![swipeGestureLeft respondsToSelector:@selector(locationInView:)]) {  
  25.         [swipeGestureLeft release];  
  26.         swipeGestureLeft = nil;  
  27.     }else {  
  28.         swipeGestureLeft.delegate = self;  
  29.         swipeGestureLeft.numberOfTouchesRequired = 1;// 手指个数 The default value is 1.  
  30.         swipeGestureLeft.direction = UISwipeGestureRecognizerDirectionLeft;// 同一个手势只能指定一个方向,不能同时指定多个方向,要指定多个方向 必须用多个手势  
  31.         [self.view addGestureRecognizer:swipeGestureLeft];  
  32.     }  
  33.       
  34.     // Up  
  35.     UISwipeGestureRecognizer *swipeGestureUp = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)];  
  36.     if (![swipeGestureUp respondsToSelector:@selector(locationInView:)]) {  
  37.         [swipeGestureUp release];  
  38.         swipeGestureUp = nil;  
  39.     }else {  
  40.         swipeGestureUp.delegate = self;  
  41.         swipeGestureUp.numberOfTouchesRequired = 1;// 手指个数 The default value is 1.  
  42.         swipeGestureUp.direction = UISwipeGestureRecognizerDirectionUp;// 同一个手势只能指定一个方向,不能同时指定多个方向,要指定多个方向 必须用多个手势  
  43.         [self.view addGestureRecognizer:swipeGestureUp];  
  44.     }  
  45.       
  46.     // Down  
  47.     UISwipeGestureRecognizer *swipeGestureDown = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)];  
  48.     if (![swipeGestureDown respondsToSelector:@selector(locationInView:)]) {  
  49.         [swipeGestureDown release];  
  50.         swipeGestureDown = nil;  
  51.     }else {  
  52.         swipeGestureDown.delegate = self;  
  53.         swipeGestureDown.numberOfTouchesRequired = 1;// 手指个数 The default value is 1.  
  54.         swipeGestureDown.direction = UISwipeGestureRecognizerDirectionDown;// 同一个手势只能指定一个方向,不能同时指定多个方向,要指定多个方向 必须用多个手势  
  55.         [self.view addGestureRecognizer:swipeGestureDown];  
  56.     }  
  57. }  
  58.   
  59. - (void)handleGesture:(UISwipeGestureRecognizer *)gestureRecognizer  
  60. {  
  61.     UIView *view = [gestureRecognizer view]; // 这个view是手势所属的view,也就是增加手势的那个view  
  62.       
  63.     /* 
  64.      direction属性: 用来指明手势滑动的方向的。 
  65.      */  
  66.     UISwipeGestureRecognizerDirection direction = gestureRecognizer.direction;  
  67.     switch (direction) {  
  68.         case UISwipeGestureRecognizerDirectionRight:  
  69.         {  
  70.             NSLog(@"direction==UISwipeGestureRecognizerDirectionRight");  
  71.             break;  
  72.         }  
  73.         case UISwipeGestureRecognizerDirectionLeft:  
  74.         {  
  75.             NSLog(@"direction==UISwipeGestureRecognizerDirectionLeft");  
  76.             break;  
  77.         }  
  78.         case UISwipeGestureRecognizerDirectionUp:  
  79.         {  
  80.             NSLog(@"direction==UISwipeGestureRecognizerDirectionUp");  
  81.             break;  
  82.         }  
  83.         case UISwipeGestureRecognizerDirectionDown:  
  84.         {  
  85.             NSLog(@"direction==UISwipeGestureRecognizerDirectionDown");  
  86.             break;  
  87.         }  
  88.         default:  
  89.             break;  
  90.     }  
  91.       
  92.       
  93.     /* 
  94.      轻扫手势 
  95.       
  96.      这个一般情况下只响应UIGestureRecognizerStateEnded || UIGestureRecognizerStateRecognized 
  97.      */  
  98.     switch (gestureRecognizer.state) {  
  99.         case UIGestureRecognizerStateEnded:{ // UIGestureRecognizerStateRecognized = UIGestureRecognizerStateEnded  
  100.             NSLog(@"======UIGestureRecognizerStateEnded || UIGestureRecognizerStateRecognized");  
  101.             break;  
  102.         }  
  103.         default:{  
  104.             NSLog(@"======Unknow gestureRecognizer");  
  105.             break;  
  106.         }  
  107.     }    
  108. }  

 

 

UIPanGestureRecognizer

 

[cpp] view plaincopy
 
  1. - (void)viewDidLoad  
  2. {  
  3.     [super viewDidLoad];  
  4.     // Do any additional setup after loading the view from its nib.  
  5.       
  6.     UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];  
  7.     view.backgroundColor = [UIColor blueColor];  
  8.     [self.view addSubview:view];  
  9.       
  10.       
  11.     UIPanGestureRecognizer *panPressGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)];  
  12.     if (![panPressGesture respondsToSelector:@selector(locationInView:)]) {  
  13.         [panPressGesture release];  
  14.         panPressGesture = nil;  
  15.     }else {  
  16.         panPressGesture.delegate = self;  
  17.         panPressGesture.maximumNumberOfTouches = NSUIntegerMax;// The default value is NSUIntegerMax.   
  18.         /* 
  19.          NSUIntegerMax : The maximum value for an NSUInteger. 
  20.          */  
  21.         panPressGesture.minimumNumberOfTouches = 1;// The default value is 1.  
  22.         [view addGestureRecognizer:panPressGesture];  
  23.     }  
  24. }  
  25.   
  26. // 拖拽手势  
  27. - (void)handleGesture:(UIPanGestureRecognizer *)gestureRecognizer  
  28. {  
  29.     UIView *view = [gestureRecognizer view]; // 这个view是手势所属的view,也就是增加手势的那个view  
  30.   
  31.       
  32.       
  33.     switch (gestureRecognizer.state) {  
  34.         case UIGestureRecognizerStateBegan:{   
  35.             NSLog(@"======UIGestureRecognizerStateBegan");  
  36.             break;  
  37.         }  
  38.         case UIGestureRecognizerStateChanged:{   
  39.             NSLog(@"======UIGestureRecognizerStateChanged");  
  40.               
  41.             /* 
  42.              让view跟着手指移动 
  43.               
  44.              1.获取每次系统捕获到的手指移动的偏移量translation 
  45.              2.根据偏移量translation算出当前view应该出现的位置 
  46.              3.设置view的新frame 
  47.              4.将translation重置为0(十分重要。否则translation每次都会叠加,很快你的view就会移除屏幕!) 
  48.              */  
  49.               
  50.             CGPoint translation = [gestureRecognizer translationInView:self.view];  
  51.             view.center = CGPointMake(gestureRecognizer.view.center.x + translation.x, gestureRecognizer.view.center.y + translation.y);  
  52.             [gestureRecognizer setTranslation:CGPointMake(0, 0) inView:self.view];//  注意一旦你完成上述的移动,将translation重置为0十分重要。否则translation每次都会叠加,很快你的view就会移除屏幕!  
  53.             break;  
  54.         }  
  55.         case UIGestureRecognizerStateCancelled:{   
  56.             NSLog(@"======UIGestureRecognizerStateCancelled");  
  57.             break;  
  58.         }  
  59.         case UIGestureRecognizerStateFailed:{   
  60.             NSLog(@"======UIGestureRecognizerStateFailed");  
  61.             break;  
  62.         }  
  63.         case UIGestureRecognizerStatePossible:{   
  64.             NSLog(@"======UIGestureRecognizerStatePossible");  
  65.             break;  
  66.         }  
  67.         case UIGestureRecognizerStateEnded:{ // UIGestureRecognizerStateRecognized = UIGestureRecognizerStateEnded  
  68.               
  69.             /* 
  70.              当手势结束后,view的减速缓冲效果 
  71.               
  72.              模拟减速写的一个很简单的方法。它遵循如下策略: 
  73.              计算速度向量的长度(i.e. magnitude) 
  74.              如果长度小于200,则减少基本速度,否则增加它。 
  75.              基于速度和滑动因子计算终点 
  76.              确定终点在视图边界内 
  77.              让视图使用动画到达最终的静止点 
  78.              使用“Ease out“动画参数,使运动速度随着时间降低 
  79.              */  
  80.               
  81.             NSLog(@"======UIGestureRecognizerStateEnded || UIGestureRecognizerStateRecognized");  
  82.               
  83.             CGPoint velocity = [gestureRecognizer velocityInView:self.view];// 分别得出x,y轴方向的速度向量长度(velocity代表按照当前速度,每秒可移动的像素个数,分xy轴两个方向)  
  84.             CGFloat magnitude = sqrtf((velocity.x * velocity.x) + (velocity.y * velocity.y));// 根据直角三角形的算法算出综合速度向量长度  
  85.               
  86.             // 如果长度小于200,则减少基本速度,否则增加它。  
  87.             CGFloat slideMult = magnitude / 200;  
  88.               
  89.             NSLog(@"magnitude: %f, slideMult: %f", magnitude, slideMult);  
  90.             float slideFactor = 0.1 * slideMult; // Increase for more of a slide  
  91.               
  92.             // 基于速度和滑动因子计算终点  
  93.             CGPoint finalPoint = CGPointMake(view.center.x + (velocity.x * slideFactor),  
  94.                                              view.center.y + (velocity.y * slideFactor));  
  95.               
  96.             // 确定终点在视图边界内  
  97.             finalPoint.x = MIN(MAX(finalPoint.x, 0), self.view.bounds.size.width);  
  98.             finalPoint.y = MIN(MAX(finalPoint.y, 0), self.view.bounds.size.height);  
  99.               
  100.             [UIView animateWithDuration:slideFactor*2 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{  
  101.                 view.center = finalPoint;    
  102.             } completion:nil];  
  103.               
  104.             break;  
  105.         }  
  106.         default:{  
  107.             NSLog(@"======Unknow gestureRecognizer");  
  108.             break;  
  109.         }  
  110.     }    
  111. }  

 

 

UILongPressGestureRecognizer

 

[cpp] view plaincopy
 
    1. - (void)viewDidLoad  
    2. {  
    3.     [super viewDidLoad];  
    4.     // Do any additional setup after loading the view from its nib.  
    5.     UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)];  
    6.     if (![longPressGesture respondsToSelector:@selector(locationInView:)]) {  
    7.         [longPressGesture release];  
    8.         longPressGesture = nil;  
    9.     }else {  
    10.         longPressGesture.delegate = self;  
    11.         longPressGesture.numberOfTapsRequired = 0;      // The default number of taps is 0.  
    12.         longPressGesture.minimumPressDuration = 0.1f;    // The default duration is is 0.5 seconds.  
    13.         longPressGesture.numberOfTouchesRequired = 1;   // The default number of fingers is 1.  
    14.         longPressGesture.allowableMovement = 10;        // The default distance is 10 pixels.  
    15.         [self.view addGestureRecognizer:longPressGesture];  
    16.     }  
    17. }  
    18.   
    19.   
    20. - (void)handleGesture:(UIGestureRecognizer *)gestureRecognizer  
    21. {  
    22.     UIView *view = [gestureRecognizer view]; // 这个view是手势所属的view,也就是增加手势的那个view  
    23.       
    24.     switch (gestureRecognizer.state) {  
    25.         case UIGestureRecognizerStateEnded:{ // UIGestureRecognizerStateRecognized = UIGestureRecognizerStateEnded  
    26.             NSLog(@"======UIGestureRecognizerStateEnded || UIGestureRecognizerStateRecognized");  
    27.             break;  
    28.         }  
    29.         case UIGestureRecognizerStateBegan:{ //   
    30.             NSLog(@"======UIGestureRecognizerStateBegan");  
    31.             break;  
    32.         }  
    33.         case UIGestureRecognizerStateChanged:{ //   
    34.             NSLog(@"======UIGestureRecognizerStateChanged");  
    35.             break;  
    36.         }  
    37.         case UIGestureRecognizerStateCancelled:{ //   
    38.             NSLog(@"======UIGestureRecognizerStateCancelled");  
    39.             break;  
    40.         }  
    41.         case UIGestureRecognizerStateFailed:{ //   
    42.             NSLog(@"======UIGestureRecognizerStateFailed");  
    43.             break;  
    44.         }  
    45.         case UIGestureRecognizerStatePossible:{ //   
    46.             NSLog(@"======UIGestureRecognizerStatePossible");  
    47.             break;  
    48.         }  
    49.         default:{  
    50.             NSLog(@"======Unknow gestureRecognizer");  
    51.             break;  
    52.         }  
    53.     }    
    54. }  
    55.   
    56. // 询问一个手势接收者是否应该开始解释执行一个触摸接收事件  
    57. - (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{  
    58.     CGPoint currentPoint = [gestureRecognizer locationInView:self.view];  
    59.     if (CGRectContainsPoint(CGRectMake(0, 0, 100, 100), currentPoint) ) {  
    60.         return YES;  
    61.     }  
    62.       
    63.     return NO;  
    64. }  
    65.   
    66. // 询问delegate,两个手势是否同时接收消息,返回YES同事接收。返回NO,不同是接收(如果另外一个手势返回YES,则并不能保证不同时接收消息)the default implementation returns NO。  
    67. // 这个函数一般在一个手势接收者要阻止另外一个手势接收自己的消息的时候调用  
    68. - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{  
    69.     return NO;  
    70. }  
    71.   
    72. // 询问delegate是否允许手势接收者接收一个touch对象  
    73. // 返回YES,则允许对这个touch对象审核,NO,则不允许。  
    74. // 这个方法在touchesBegan:withEvent:之前调用,为一个新的touch对象进行调用  
    75. - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{  
    76.     return YES;  

 

转自:http://blog.csdn.net/namehzf/article/details/7424882

手势知多少

标签:

原文地址:http://www.cnblogs.com/mumoozhu/p/4600342.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!