cocos2d-iOS - 手势识别器 [英] cocos2d-iOS - Gesture recognisers

查看:93
本文介绍了cocos2d-iOS - 手势识别器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人设法在cocos-2d中进行手势识别工作吗?

Has anyone managed to get the gesture recognition working in cocos-2d?

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

I have read a post here that claimed to have achieved it, here: http://www.cocos2d-iphone.org/forum/topic/8929

我从git中心修补了这里: https://github.com/xemus / cocos2d-GestureRecognizers / blob / master / README

I patched from the git hub here: 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 :( ");
}

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

My tap event never gets called.

有人有这个工作吗?手动识别手势识别是否有困难?

Has anyone got this working? How difficult is it to do gesture recognition manually for swipe detection?

推荐答案

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

You need to attach the gesture recognizer to something "up the chain". Don't attach them to the individual nodes; attach them to the UIView (i.e., [[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,但你可以很容易地将其抽象为传入的对象。

This particular code is used in a superclass for scene controllers, so the target for the selector is hard-coded to "self", but you could easily abstract that to a passed-in object. Also, you could extrapolate the above to easily create gesture recognizers for taps, pinches, etc.

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

In the subclass for the controller, then, I just do this:

- (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;
    }

}

这篇关于cocos2d-iOS - 手势识别器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆