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

来源:网络 责任编辑:栏目编辑 发表时间:2013-07-02 00:12 点击:

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

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

1) 单击、双击处理

[html] 
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
{  
    //Get all the touches.  
    NSSet *allTouches = [event allTouches];  
    //Number of touches on the screen  
    switch ([allTouches count])  
    {  
        case 1:  
        {  
            //Get the first touch.  
            UITouch *touch = [[allTouches allObjects] objectAtIndex:0];  
            switch([touch tapCount])  
            {  
                case 1://Single tap  
                    // 单击!!  
                    break;  
                case 2://Double tap.  
                    // 双击!!  
                    break; }  
            }  
            break;  
        } 
    } 

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

[html] 
//计算两个点之间的距离函数 
- (CGFloat)distanceBetweenTwoPoints:(CGPoint)fromPoint toPoint:(CGPoint)toPoint 

    float x = toPoint.x - fromPoint.x; 
    float y = toPoint.y - fromPoint.y; 
    return sqrt(x * x + y * y); 

 
//记录多触点之间的初始距离 
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 

    NSSet *allTouches = [event allTouches]; 
    switch ([allTouches count]) 
    { 
        case 1: { //Single touch 
            break;} 
        case 2: { //Double Touch 
            //Track the initial distance between two fingers. 
            UITouch *touch1 = [[allTouches allObjects] objectAtIndex:0]; 
            UITouch *touch2 = [[allTouches allObjects] objectAtIndex:1]; 
            initialDistance = [self distanceBetweenTwoPoints:[touch1 locationInView:[self view]] toPoint:[touch2 locationInView:[self view]]]; 
            } 
        &nb

    相关新闻>>

      发表评论
      请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
      用户名: 验证码:点击我更换图片
      最新评论 更多>>

      推荐热点

      • cocos2d-x学习笔记(19)--label 、label atlas
      • cocos2d-x学习笔记(23)--地图的使用3--CCTMXLayer
      • Cocos2d-x学习(一):HelloWorld
      • cocos2dx在xcode下开发,编译到android上(2)
      • cocos2d 设置屏幕默认方向
      • cocos2d-x学习笔记(22)--地图的使用2(TMX) --Z-Order、AnchorPoi
      • Cocos2d-x 2.0 之 Actions “三板斧” 之一
      • cocos2d-x学习笔记(18)--游戏打包(windows平台)
      • cocos2d-x学习笔记(16)--spritesheet(精灵表单)
      网站首页 - 友情链接 - 网站地图 - TAG标签 - RSS订阅 - 内容搜索
      Copyright © 2008-2015 计算机技术学习交流网. 版权所有

      豫ICP备11007008号-1