Ios 如何向skView添加手势?

Ios 如何向skView添加手势?,ios,sprite-kit,uigesturerecognizer,Ios,Sprite Kit,Uigesturerecognizer,我需要在游戏场景中添加几个滑动手势。据我所知,这只能通过编程实现,不幸的是,我从未以这种方式添加手势,我一直使用IB。我知道我需要初始化手势识别器,使用initWithTarget:action:,我知道如何设置其属性,但我不知道如何使此手势执行某些操作。我假设它是通过动作参数@selector实现的,但这似乎从未被调用过。我做错了吗?以下是我所拥有的: UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc

我需要在游戏场景中添加几个滑动手势。据我所知,这只能通过编程实现,不幸的是,我从未以这种方式添加手势,我一直使用IB。我知道我需要初始化手势识别器,使用
initWithTarget:action:
,我知道如何设置其属性,但我不知道如何使此手势执行某些操作。我假设它是通过动作参数
@selector
实现的,但这似乎从未被调用过。我做错了吗?以下是我所拥有的:

UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self
                                        action:@selector(animateSwipeRightLeft)];
[self.view addGestureRecognizer:swipeRight];
对于选择器,我有:

-(void)animateSwipeRightLeft {

    //do stuff...
}
所以它归结为几个相关的问题:我的设置正确吗?如果是,为什么没有调用我的选择器?如果我对IB做不到这一点是错误的,怎么做


另外,如果有帮助的话,我的手势会在我的场景的initWithSize方法中设置。

您应该在
SKScene
didMoveToView
方法中添加手势识别器

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

self.view
is
nil
inside
init
methods

在添加识别器时检查self.view是否为nil。在节点的init方法中它将是nil。@LearnCos2D它是nil,那么如何修复它?在init之后添加手势识别器,或者在创建当前包含AddGestureRecognitor代码的节点的位置添加手势识别器