在SKScene中的UIPanGestureRecognizer

我一直在使用UIGestureRecognizersSKScene/SKNode'sSKScene/SKNode's进行SpriteKit 。 我有一个问题,我已经接近修复它,但我对一件事感到困惑。 基本上,我有一个平移手势识别器,允许用户在屏幕上拖动一个精灵。

我遇到的唯一问题是,只需轻轻点击一下,即可初始化平移手势,然后只有第二次点击才能正常工作。 我在想,这是因为我的平移手势是在touchesBegan初始化的。 但是,我不知道在SKScene的initWithSize方法中初始化它之后,还有什么地方停止了手势识别器的实际工作。

 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { if (!self.pan) { self.pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(dragPlayer:)]; self.pan.minimumNumberOfTouches = 1; self.pan.delegate = self; [self.view addGestureRecognizer:self.pan]; } } -(void)dragPlayer: (UIPanGestureRecognizer *)gesture { CGPoint trans = [gesture translationInView:self.view]; SKAction *moveAction = [SKAction moveByX:trans.xy:-trans.y duration:0]; [self.player runAction:move]; [gesture setTranslation:CGPointMake(0, 0) inView:self.view]; } 

这是因为你开始添加触摸手势,所以手势不会存在,直到屏幕被点击至less一次。 另外,我会validation你实际上是使用initWithSize:作为你的初始化程序,因为你不应该在那里添加手势有任何问题。

另一个select是移动代码,将手势添加到-[SKScene didMovetoView:] ,在场景出现后立即调用。 更多信息在文档中 。

 - (void)didMoveToView:(SKView *)view { [super didMoveToView:view]; // add gesture here! } 

这是我的第一篇文章! 希望不要绊倒我自己的脚趾…

嗨,大家好,所以我遇到了UISwipeGestureRecognizer无法正常工作的问题。 我正在初始化它在我的initWithSize方法,所以根据这个职位,我把它移动到我的didMoveToView方法。 现在它工作(谢谢0x7fffffff)。 我所做的只是从一个方法中删除了下面两行,然后粘贴到另一个方法中。

 _warpGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(warpToNextLevel:)]; [self.view addGestureRecognizer:_warpGesture]; 

在我的“调查”中,我遇到了userInteractionEnabled,并尝试将其设置为YES在我的initWithSize方法…

 self.view.userInteractionEnabled = YES; NSLog(@"User interaction enabled %s", self.view.userInteractionEnabled ? "Yes" : "No"); 

即使我只是将其设置为YES,这也会logging为NO。 进一步的调查发现,如果我不尝试手动设置userInteractionEnabled,那么在initWithSize(我似乎无法改变这个,如果我想),并自动设置为YES当我在didMoveToView是没有。

这一切都与我相关,但我希望知道的人能够解释这里发生的事情。 谢谢!