how to add a gesture to skView?

对着背影说爱祢 提交于 2019-12-13 04:21:56

问题


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:


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



来源:https://stackoverflow.com/questions/24295734/how-to-add-a-gesture-to-skview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!