Cocos2d中添加手势支持的三种方法


最近一直琢磨在Cocos2d里添加手势的功能,找了一些资料加上自己的理解,整理出了三种方法和大家分享。

第一种,很简单,就是知易cocos2d-iPhone教程-04所介绍的(其实这并不是真正的手势,只是也能实现部分手势功能而已),代码如下:

1) 单击、双击处理

  1. - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event  
  2. {   
  3.     //Get all the touches.   
  4.     NSSet *allTouches = [event allTouches];   
  5.     //Number of touches on the screen   
  6.     switch ([allTouches count])   
  7.     {   
  8.         case 1:   
  9.         {   
  10.             //Get the first touch.   
  11.             UITouch *touch = [[allTouches allObjects] objectAtIndex:0];   
  12.             switch([touch tapCount])   
  13.             {   
  14.                 case 1://Single tap   
  15.                     // 单击!!   
  16.                     break;   
  17.                 case 2://Double tap.   
  18.                     // 双击!!   
  19.                     break; }   
  20.             }   
  21.             break;   
  22.         }  
  23.     }  
  24. }  

2) 两个指头的分开、合拢手势。

  1. //计算两个点之间的距离函数  
  2. - (CGFloat)distanceBetweenTwoPoints:(CGPoint)fromPoint toPoint:(CGPoint)toPoint  
  3. {  
  4.     float x = toPoint.x - fromPoint.x;  
  5.     float y = toPoint.y - fromPoint.y;  
  6.     return sqrt(x * x + y * y);  
  7. }  
  8.   
  9. //记录多触点之间的初始距离  
  10. - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event  
  11. {  
  12.     NSSet *allTouches = [event allTouches];  
  13.     switch ([allTouches count])  
  14.     {  
  15.         case 1: { //Single touch  
  16.             break;}  
  17.         case 2: { //Double Touch  
  18.             //Track the initial distance between two fingers.  
  19.             UITouch *touch1 = [[allTouches allObjects] objectAtIndex:0];  
  20.             UITouch *touch2 = [[allTouches allObjects] objectAtIndex:1];  
  21.             initialDistance = [self distanceBetweenTwoPoints:[touch1 locationInView:[self view]] toPoint:[touch2 locationInView:[self view]]];  
  22.             }  
  23.             break;  
  24.         default:  
  25.             break;  
  26.     }  
  27. }  
  28.   
  29. //两个指头移劢时,判断是分开还是合拢  
  30. - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event  
  31. {  
  32.     NSSet *allTouches = [event allTouches];  
  33.     switch ([allTouches count])  
  34.     {  
  35.         case 1:  
  36.             break;  
  37.         case 2:{  
  38.             UITouch *touch1 = [[allTouches allObjects] objectAtIndex:0];  
  39.             UITouch *touch2 = [[allTouches allObjects] objectAtIndex:1];  
  40.             //Calculate the distance between the two fingers.  
  41.             CGFloat finalDistance = [self distanceBetweenTwoPoints: [touch1 locationInView:[self view]] toPoint:[touch2 locationInView:[self view]]];  
  42.             //Check if zoom in or zoom out.  
  43.             if(initialDistance > finalDistance) {  
  44.                 NSLog(@"Zoom Out"); // 合拢!!  
  45.             }  
  46.             else {  
  47.                 NSLog(@"Zoom In"); // 分开!!  
  48.             }  
  49.             }   
  50.             break;  
  51.     }  
  52. }  
  • 1
  • 2
  • 3
  • 下一页

相关内容