【问题标题】:cocos2d-iOS - Gesture recogniserscocos2d-iOS - 手势识别器
【发布时间】:2011-02-13 18:13:06
【问题描述】:

有没有人设法让手势识别在 cocos-2d 中工作?

我在这里读过一篇声称已经实现的帖子,在这里:http://www.cocos2d-iphone.org/forum/topic/8929

我从这里的 git hub 打了补丁:https://github.com/xemus/cocos2d-GestureRecognizers/blob/master/README

我创建了CCSprite 的子类(这是CCNode 的子类):

-(id) initWithTexture:(CCTexture2D*)texture rect:(CGRect)rect {
if( (self=[super initWithTexture:texture rect:rect]) )
{
    CCGestureRecognizer* recognizer;
    recognizer = [CCGestureRecognizer 
        CCRecognizerWithRecognizerTargetAction:[[[UITapGestureRecognizer alloc]init] autorelease] 
                  target:self 
                  action:@selector(tap:node:)];
    [self addGestureRecognizer:recognizer];
}
return self;
}

委托方法:

- (void) swipe:(UIGestureRecognizer*)recognizer node:(CCNode*)node
{
NSLog(@" I never get called :( ");
}

我的点击事件永远不会被调用。

有人搞定这个吗?手动进行手势识别进行滑动检测有多难?

【问题讨论】:

    标签: iphone cocos2d-iphone


    【解决方案1】:

    您需要将手势识别器附加到“链上”的东西。不要将它们附加到单个节点;将它们附加到 UIView(即 [[CCDirector sharedDirector] openGLView])。

    这就是我所做的:

    - (UIPanGestureRecognizer *)watchForPan:(SEL)selector number:(int)tapsRequired {
        UIPanGestureRecognizer *recognizer = [[[UIPanGestureRecognizer alloc] initWithTarget:self action:selector] autorelease];
        recognizer.minimumNumberOfTouches = tapsRequired;
        [[[CCDirector sharedDirector] openGLView] addGestureRecognizer:recognizer];
        return recognizer;
    }
    
    - (void)unwatch:(UIGestureRecognizer *)gr {
        [[[CCDirector sharedDirector] openGLView] removeGestureRecognizer:gr];
    }
    

    此特定代码用于场景控制器的超类中,因此选择器的目标被硬编码为“self”,但您可以轻松地将其抽象为传入的对象。此外,您可以推断上述内容以轻松创建轻击、捏合等手势识别器。

    在控制器的子类中,我只是这样做:

    - (MyController *)init {
        if ((self = [super init])) {
            [self watchForPan:@selector(panning:) number:1];
        }
        return self;
    }
    
    - (void)panning:(UIPanGestureRecognizer *)recognizer {
    
        CGPoint p;
        CGPoint v;
    
        switch( recognizer.state ) {
            case UIGestureRecognizerStatePossible:
            case UIGestureRecognizerStateBegan:
                p = [recognizer locationInView:[CCDirector sharedDirector].openGLView];
                (do something when the pan begins)
                break;
            case UIGestureRecognizerStateChanged:
                p = [recognizer locationInView:[CCDirector sharedDirector].openGLView];
                (do something while the pan is in progress)
                break;
            case UIGestureRecognizerStateFailed:
                break;
            case UIGestureRecognizerStateEnded:
            case UIGestureRecognizerStateCancelled:
                (do something when the pan ends)
                (the below gets the velocity; good for letting player "fling" things)
                v = [recognizer velocityInView:[CCDirector sharedDirector].openGLView];
                break;
        }
    
    }
    

    【讨论】:

    • +1 for cc's answer above,这是正确的方法。您可以通过这种方式使用所有 UIGestureRecognizer 子类来检测平移、捏合、点击手势。您还可以使用带有状态案例 UIGestureRecognizerStateChanged 的​​ UILongPressGestureRecognizer 来检测拖动。
    • 漂亮干净,可爱。两个小问题: 1. locationInView 返回 UIKit 坐标系中的坐标,使用前需要转换为 GL 坐标,例如:p = [[CCDirector sharedDirector] convertToGL: [recognizer locationInView:[CCDirector sharedDirector].view]]; 2. openGLView 从 Cocos2D 2.0 开始被弃用,已被[CCDirector sharedDirector].view 取代。
    • 非常感谢您发布这个:)
    【解决方案2】:

    如果您不想手动处理所有事情,我创建了一个简单的类别,可以将手势识别器添加到任何 cocos2d 版本 阅读更多:

    http://www.merowing.info/2012/03/using-gesturerecognizers-in-cocos2d/

    或从 github 获取它

    https://github.com/krzysztofzablocki/CCNode-SFGestureRecognizers

    【讨论】:

    • 这太棒了!只要确保使用 ccTouches 方法在节点上方制作带有手势识别器的节点即可。
    • 在 Cocos2d 3.0 中没有 [YourCCSprite addGestureRecognizer:YourGestureRecognizer] 的可见界面;那么有什么解决办法吗?
    • 是的,Cocos2d 3.0的gestureRecognizer类会有更新吗???这真的很重要,也是一个缺失的部分。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多