问题 SKScene的UIPanGestureRecognizer


我一直在试验 UIGestureRecognizers  和新的 SKScene/SKNode's 在 SpriteKit。我有一个问题,我接近修复它但我对一件事感到困惑。基本上,我有一个平移手势识别器,允许用户在屏幕上拖动精灵。

我遇到的唯一问题是,实际初始化平移手势需要一次点击,然后只有在SECOND上点击才能正常工作。我想这是因为我的平移手势已经初始化了 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];
    }

2459
2017-09-26 23:27


起源



答案:


那是因为你在触摸开始时添加了手势,所以在屏幕至少被点击一次之前手势就不存在了。另外,我会验证您实际上是在使用initWithSize:作为初始化程序,因为在那里添加手势时不应该有任何问题。

另一种选择是移动代码以添加手势 -[SKScene didMovetoView:] 在场景出现后立即调用。更多信息 在文档中

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

11
2017-09-26 23:33





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

大家好,所以我遇到了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。

这一切都让我觉得相关,但我希望知道的人解释这里发生了什么。谢谢!


1
2018-04-02 14:58