SKScene的UIPanGestureRecognizer [英] UIPanGestureRecognizer in SKScene

查看:76
本文介绍了SKScene的UIPanGestureRecognizer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've been experimenting with UIGestureRecognizers and the new SKScene/SKNode's in SpriteKit. I've had one problem, and I got close to fixing it but I am confused on one thing. Essentially, I have a pan gesture recognizer that allows the user to drag a sprite on the screen.

我遇到的一个问题是,实际初始化平移手势需要一次点击,然后只有在SECOND上点击它才能正常工作。我想这是因为我的平移手势是在 touchesBegan 中初始化的。但是,我不知道在其他地方放置它,因为在SKScene的 initWithSize 方法中初始化它会使手势识别器停止实际工作。

The single problem I have is that it takes ONE tap to actually initialize the pan gesture, and then only on the SECOND tap on it works correctly. I'm thinking that this is because my pan gesture is initialized in touchesBegan. However, I don't know where else to put it since initializing it in the SKScene's initWithSize method stopped the gesture recognizer from actually working.

-(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:作为初始化程序,因为在那里添加手势不会有任何问题。

That's because you're adding the gesture in touches began, so the gesture doesn't exist until the screen has been tapped at least once. Additionally, I would verify that you're actually using initWithSize: as your initializer, because you shouldn't have any problems adding the gesture there.

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

Another option is to move the code to add the gesture into -[SKScene didMovetoView:] which gets called immediately after the scene has been presented. More info in the docs.

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

这篇关于SKScene的UIPanGestureRecognizer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆