UIPanGestureRecognizer in SKScene

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.

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. Here is my code:

  • Xcode 8 red files but not missing
  • Beat Matching Algorithm
  • self = revisited
  • UICollectionView scrolling in both directions
  • How to open Settings programmatically like in Facebook app?
  • iOS - Parsing JSON Dictionary with NSJSONSerialization in Swift
  • -(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];

    2 Solutions Collect From Internet About “UIPanGestureRecognizer in SKScene”

    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.

    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!

    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. I was initializing it in my initWithSize method so based on this post I moved it to my didMoveToView method. Now it works (thanks 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…

    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. 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.

    This all strikes me as relevant but I would love for someone in the know to explain just what’s going on here. Thanks!