天堂向右,我依然向左

天下之大,虽离家千里,何处不可往!何事不可为!
生活之路,纵坎坷曲折,当奋斗不息,则精彩纷呈!

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

 

不知道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.     }  
posted on 2010-08-24 15:53  老舟  阅读(477)  评论(0编辑  收藏  举报