不知道4.0SDK带有手势的直接支持没有,至少3.2已经可以用了.但是如果想支持早期的版本,那么手势的识别无疑是一种痛苦,因为需要自己写代码来判定手势...

 

下面代码是判断一个滑动的手势(swipe),虽然很简单但是总体思想就是这样了.当在一个水平,或者纵向滑动时给出一个滑动距离以及偏移量.当实际滑动距离超过指定的距离,且水平或者纵向的偏移量小于指定的偏移量则视为这个滑动手势判定成功!

 

Java代码 
  1. - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event   
  2. {   
  3.     UITouch *touch = touches.anyObject;   
  4.     CGPoint currentTouchPosition = [touch locationInView:self];   
  5.       
  6.     if (fabsf(startTouchPosition.x - currentTouchPosition.x) >=   
  7.         HORIZ_SWIPE_DRAG_MIN &&   
  8.         fabsf(startTouchPosition.y - currentTouchPosition.y) <=   
  9.         VERT_SWIPE_DRAG_MAX)   
  10.     {   
  11.         // Horizontal Swipe  
  12.         if (startTouchPosition.x < currentTouchPosition.x) {  
  13.             NSLog(@"from left");  
  14.             dirString = kCATransitionFromLeft;  
  15.         }  
  16.         else   
  17.             NSLog(@"from right");  
  18.             dirString = kCATransitionFromRight;  
  19.     }   
  20.     else if (fabsf(startTouchPosition.y - currentTouchPosition.y) >=   
  21.              HORIZ_SWIPE_DRAG_MIN &&   
  22.              fabsf(startTouchPosition.x - currentTouchPosition.x) <=   
  23.              VERT_SWIPE_DRAG_MAX)  
  24.     {   
  25.         // Vertical Swipe  
  26.         if (startTouchPosition.y < currentTouchPosition.y)   
  27.             dirString = kCATransitionFromBottom;  
  28.         else   
  29.             dirString = kCATransitionFromTop;  
  30.     } else   
  31.     {  
  32.         // Process a non-swipe event.   
  33.         // dirString = NULL;  
  34.     }  
  35. }   
  36.   
  37. - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event   
  38. {  
  39.     if (dirString)   
  40.     {  
  41.         // do it      
  42.     }  

相关文章:

  • 2021-12-22
  • 2021-12-22
  • 2021-10-14
  • 2021-06-12
  • 2022-01-31
  • 2022-12-23
  • 2022-01-02
猜你喜欢
  • 2021-12-05
  • 2022-12-23
  • 2022-12-23
  • 2021-12-31
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案