简体   繁体   English

SKScene的UIPanGestureRecognizer

[英]UIPanGestureRecognizer in SKScene

I've been experimenting with UIGestureRecognizers and the new SKScene/SKNode's in SpriteKit . 我一直在尝试使用UIGestureRecognizersSKScene/SKNode'sSKScene/SKNode's 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. 基本上,我有一个平移手势识别器,允许用户在屏幕上拖动精灵。

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. 我遇到的唯一问题是,实际初始化平移手势需要一次点击,然后只有在SECOND上点击它才能正常工作。 I'm thinking that this is because my pan gesture is initialized in touchesBegan . 我想这是因为我的pan手势在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. 但是,我不知道在哪里放置它,因为在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];
    }

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. 另外,我会验证您实际上是在使用initWithSize:作为初始化程序,因为在那里添加手势应该没有任何问题。

Another option is to move the code to add the gesture into -[SKScene didMovetoView:] which gets called immediately after the scene has been presented. 另一种选择是移动代码以将手势添加到-[SKScene didMovetoView:] ,在场景出现后立即调用。 More info in the docs . 更多信息在文档中

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

This is my first post! 这是我的第一篇文章! Hoping to not trip over my own toes... 希望不要绊倒我自己的脚趾......

Hi guys, so I was having an issue with a UISwipeGestureRecognizer not working. 大家好,所以我遇到了UISwipeGestureRecognizer无法正常工作的问题。 I was initializing it in my initWithSize method so based on this post I moved it to my didMoveToView method. 我在我的initWithSize方法中初始化它所以基于这篇文章我将它移动到我的didMoveToView方法。 Now it works (thanks 0x7fffffff). 现在它可以工作(感谢0x7fffffff)。 All I did was cut the following two lines from one method and paste them in the other. 我所做的就是从一种方法中剪切以下两行,然后将它们粘贴到另一种方法中。

_warpGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(warpToNextLevel:)];
[self.view addGestureRecognizer:_warpGesture];

In my "investigation" I came across userInteractionEnabled and tried to set it to YES in my initWithSize method... 在我的“调查”中,我遇到了userInteractionEnabled并尝试在我的initWithSize方法中将其设置为YES ...

self.view.userInteractionEnabled = YES;
NSLog(@"User interaction enabled %s", self.view.userInteractionEnabled ? "Yes" : "No");

This would log NO even though i'd just set it to YES. 即使我将其设置为YES,这也会记录NO。 Further investigation found that if I don't try to manually set userInteractionEnabled then it's NO during initWithSize (I can't seem to change this if I want to) and automatically gets set to YES when i'm in didMoveToView. 进一步的调查发现,如果我不尝试手动设置userInteractionEnabled,那么在initWithSize期间它是NO(如果我想的话我似乎无法改变它)并且当我在didMoveToView时自动设置为YES。

This all strikes me as relevant but I would love for someone in the know to explain just what's going on here. 这一切都让我觉得相关,但我希望知道的人解释这里发生了什么。 Thanks! 谢谢!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM