cocos2d-iOS - Gesture recognisers(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 hub 打了补丁: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
的子类):
I made a subclass of CCSprite
(which is a subclass of 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 - 手势识别器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:cocos2d-iOS - 手势识别器
- MalformedJsonException:在第1行第1列路径中使用JsonReader.setLenient(True)接受格式错误的JSON 2022-01-01
- Android - 拆分 Drawable 2022-01-01
- android 4中的android RadioButton问题 2022-01-01
- Android viewpager检测滑动超出范围 2022-01-01
- 想使用ViewPager,无法识别android.support.*? 2022-01-01
- Android - 我如何找出用户有多少未读电子邮件? 2022-01-01
- 使用自定义动画时在 iOS9 上忽略 edgesForExtendedLayout 2022-01-01
- 在测试浓缩咖啡时,Android设备不会在屏幕上启动活动 2022-01-01
- 用 Swift 实现 UITextFieldDelegate 2022-01-01
- 如何检查发送到 Android 应用程序的 Firebase 消息的传递状态? 2022-01-01