如何实现手势识别功能?

大家知道如何实现cocos2d中的手势识别吗?我在一篇文章中,看到过类似效果的实现方法:
http://www.cocos2d-iphone.org/forum/topic/8929
在github上找到了相关的补充资料:
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;
}

delegate方法:

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

在这种方法下,我设定的点击事件(tap event)始终不响应。
大家遇到过类似的问题吗?在cocos2d中实现swipe detection的手势识别很困难吗?

原问题:cocos2d-iOS - Gesture recognisers

阅读 4.6k
2 个回答

答:cc.
你需要将手势识别器(gesture recognizer)添加到"up the chain"中。不要只添加这些代码,而是要将他们和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];
}

这段特殊的代码是用在超场景控制器中(scene controller),所以选择器的目标是筛选出,可以进行“自我”硬式编码(hard-code)的代码。
对于控制器的子类别,我是这样做的:

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

}

答:Krzysztof Zabłocki
如果你不想人为控制一切操作,我建议你可以创建一种简单的类别,用来添加手势识别功能,详情可查看:
http://www.merowing.info/2012/03/using-gesturerecognizers-in-cocos2d/
或者可以在github上找到这种功能的实现源码:https://github.com/krzysztofzablocki/CCNode-SFGestureRecognizers

在ios端,我们的开发在构建这个手势密码的时候,我这边提供了@2x的圆圈给它,但是开发输出出来,还是有模糊像素的问题手势密码图形模糊

我如何向开发进行建议呢,还是说设计的圆圈本身在ios端的显示存在问题

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进