当前位置:编程学习 > wap >>

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

最近一直琢磨在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]]]; 
            } 
            break; 
        default: 
            break; 
    } 

 
//两个指头移劢时,判断是分开还是合拢 
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 

    NSSet *allTouches = [event allTouches]; 
    switch ([allTouches count]) 
    { 
        case 1: 
            break; 
        case 2:{ 
            UITouch *touch1 = [[allTouches allObjects] objectAtIndex:0]; 
            UITouch *touch2 = [[allTouches allObjects] objectAtIndex:1]; 
            //Calculate the distance between the two fingers. 
            CGFloat finalDistance = [self distanceBetweenTwoPoints: [touch1 locationInView:[self view]] toPoint:[touch2 locationInView:[self view]]]; 
            //Check if zoom in or zoom out. 
            if(initialDistance > finalDistance) { 
                NSLog(@"Zoom Out"); // 合拢!! 
            } 
            else { 
                NSLog(@"Zoom In"); // 分开!! 
            } 
            }  
            break; 
    } 

第二种,是在Cocoa China中找的一种办法,它的原理是通过修改CCLayer,CCNode两个Cocos2d类的源码实现手势支持:

1.首先要修改两个Cocos2d类的源码分别为CCLayer,CCNode

2.增加手势类源码 CCGestureRecognizer

以上三个类的源码,可以在我的资源中找着(地址:http://download.csdn.net/detail/wangqiuyun/4460442),(记住CCLayer与CCNode要覆盖原来的文件)
CCGestureRecognizer.h与.m要拷贝到当前工程的libs/cocos2d/Platforms/iOS目录下
在工程文件中加入CCGestureRecognizer.h与.m

3.完成以上工作后所有Node的子类中都可以使用手势了,如在HelloWorld工程中:

1)修改HelloWorldLayer.m中的init方法加入以下代码:

[html] 
//定义响应的手势类(支持所有UI手势) 
UILongPressGestureRecognizer * longPress = [[[UILongPressGestureRecognizer alloc] init] autorelease]; 
longPress.minimumPressDuration = 0.5f; 
longPress.allowableMovement = 5.0f; 
//将UI手势对象longPress作为CCGestureRecognizer类的初始化参数 
//@selector(longPress:node:) 为响应手势的触发方法 
CCGestureRecognizer * rescognizer = [CCGestureRecognizer CCRecognizerWithRecognizerTargetAction:longPress target:self action:@selector(longPress:node:)]; 
//设置手势类的代理 
rescognizer.delegate = self; 
//为self (当前为CCLayer对象)注册手势 
[self addGestureRecognizer:rescognizer]; 
//必须设置self可接收Touch事件 
self.isTouchEnabled = YES; 
2)另需要增加响应方法的实现:

[html] 
-(void)

补充:移动开发 , 其他 ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,