ios – UIPanGestureRecognizer在SKScene

我一直在使用UIGestureRecognizers和SpriteKit中的新SKScene / SKNode.我遇到了一个问题,我已经接近修复了,但是我对此感到困惑.本质上,我有一个平移手势识别器,允许用户拖动一个精灵在屏幕上.

我遇到的一个问题是,需要一个轻点实际初始化泛手势,然后只有在其上的第二个点击它正常工作.我在想,这是因为我的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:].更多信息in the docs.

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

相关文章

当我们远离最新的 iOS 16 更新版本时,我们听到了困扰 Apple...
欧版/美版 特别说一下,美版选错了 可能会永久丧失4G,不过只...
一般在接外包的时候, 通常第三方需要安装你的app进行测...
前言为了让更多的人永远记住12月13日,各大厂都在这一天将应...