我一直在试验UIGestureRecognizers和SpriteKit的新SKScene/SKNode's。我有一个问题,我已经接近解决它,但我困惑于一件事。本质上,我有一个pan手势识别器,允许用户在屏幕上拖动一个精灵。
唯一的问题是,它需要一个点击来实际初始化pan手势,然后只有对它的第二个点击正确工作。我认为这是因为我的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];
}发布于 2013-09-26 23:33:47
这是因为你要在触摸开始时添加手势,所以这个手势在屏幕至少被点击一次后才会存在。此外,我将验证您是否实际使用initWithSize:作为您的初始化工具,因为您在那里添加这个手势不应该有任何问题。
另一种选择是移动代码,将手势添加到-[SKScene didMovetoView:]中,在场景出现后立即调用该手势。更多信息,在医生里。
- (void)didMoveToView:(SKView *)view
{
[super didMoveToView:view];
// add gesture here!
}发布于 2014-04-02 14:58:15
这是我的第一篇帖子!希望不要被我自己的脚趾绊倒..。
嗨,伙计们,我对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");这将日志不,即使我只是把它设置为是。进一步的调查发现,如果我不尝试手动设置userInteractionEnabled,那么在initWithSize期间它是否的(如果我想要的话,我似乎不能改变它),并且在initWithSize中自动设置为YES。
这一切给我的印象都是相关的,但我希望有一个知道的人解释一下这里发生了什么。谢谢!
https://stackoverflow.com/questions/19040347
复制相似问题