你好,游客 登录 注册 搜索
背景:
阅读新闻

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

[日期:2012-08-11] 来源:Linux社区  作者:wangqiuyun [字体:大 中 小]

最近一直琢磨在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. }  
linux
相关资讯       Cocos2d 
本文评论   查看全部评论 (1)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款
第 1 楼
* 匿名 发表于 2013/5/21 17:51:30
第二种方法下载了ccnode和ccolayer后,感觉版本比较旧了,会出现错误,可否改成对应cocos2d 2.0以上版本的。
第三种方法也试了下,没有任何错误,但是完全不响应事件。求高人解释。