【问题标题】:UIPanGestureRecognizer in SKSceneSKScene 中的 UIPanGestureRecognizer
【发布时间】:2013-09-26 23:27:36
【问题描述】:

我一直在试验UIGestureRecognizersSpriteKit 中的新SKScene/SKNode's。我遇到了一个问题,我接近解决它,但我对一件事感到困惑。本质上,我有一个平移手势识别器,允许用户在屏幕上拖动精灵。

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

【问题讨论】:

    标签: ios objective-c uigesturerecognizer sprite-kit


    【解决方案1】:

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

    另一个选项是移动代码以将手势添加到-[SKScene didMovetoView:],它会在场景呈现后立即调用。更多信息in the docs.

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

    【讨论】:

      【解决方案2】:

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

      大家好,我遇到了 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 期间为 NO(如果我愿意,我似乎无法更改此设置)并且当我在 didMoveToView 中时自动设置为 YES。

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-05-17
        • 2018-08-06
        • 1970-01-01
        • 2015-02-24
        • 2014-08-24
        • 1970-01-01
        • 2011-08-13
        • 1970-01-01
        相关资源
        最近更新 更多