[英]UIPanGestureRecognizer in SKScene
我一直在尝试使用UIGestureRecognizers
和SKScene/SKNode's
新SKScene/SKNode's
SpriteKit
。 我有一个问题,我接近修复它但我对一件事感到困惑。 基本上,我有一个平移手势识别器,允许用户在屏幕上拖动精灵。
我遇到的唯一问题是,实际初始化平移手势需要一次点击,然后只有在SECOND上点击它才能正常工作。 我想这是因为我的pan手势在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.x y:-trans.y duration:0];
[self.player runAction:move];
[gesture setTranslation:CGPointMake(0, 0) inView:self.view];
}
那是因为你在触摸开始时添加了手势,所以在屏幕被点击至少一次之前手势就不存在了。 另外,我会验证您实际上是在使用initWithSize:作为初始化程序,因为在那里添加手势应该没有任何问题。
另一种选择是移动代码以将手势添加到-[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并尝试在我的initWithSize方法中将其设置为YES ...
self.view.userInteractionEnabled = YES;
NSLog(@"User interaction enabled %s", self.view.userInteractionEnabled ? "Yes" : "No");
即使我将其设置为YES,这也会记录NO。 进一步的调查发现,如果我不尝试手动设置userInteractionEnabled,那么在initWithSize期间它是NO(如果我想的话我似乎无法改变它)并且当我在didMoveToView时自动设置为YES。
这一切都让我觉得相关,但我希望知道的人解释这里发生了什么。 谢谢!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.