0

I need to add several swipe gestures to my scene on a game. From what I know, this can only be done programmatically, unfortunately I have never added gestures in this way, I have always used IB. I know I need to initialize a gesture recognizer, using initWithTarget:action: and I know how to set its properties, what I don't know is how to make this gesture do stuff. I assume it is through the action parameter @selector but this seems to never get called. Am I doing this wrong? here is what I have:

UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self
                                        action:@selector(animateSwipeRightLeft)];
[self.view addGestureRecognizer:swipeRight];

and for the selector I have:

-(void)animateSwipeRightLeft {

    //do stuff...
}

so it comes down to a few related questions: am I setting this up correctly? if so, why is my selector not being called? and if I am wrong about not being able to do this with IB, how?

also, if it helps, my gestures are set up in the initWithSize method for my scene.

1 Answers1

0

You should add gesture recognizers in SKScene's didMoveToView method.

- (void)didMoveToView:(SKView *)view {
    UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self
                                    action:@selector(animateSwipeRightLeft)];
    [self.view addGestureRecognizer:swipeRight];
}

self.view is nil inside init methods

Andrey Gordeev
  • 27,503
  • 11
  • 123
  • 150