如何向skView添加手势? [英] how to add a gesture to skView?

查看:25
本文介绍了如何向skView添加手势?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在游戏场景中添加多个滑动手势.据我所知,这只能以编程方式完成,不幸的是我从未以这种方式添加手势,我一直使用IB.我知道我需要初始化一个手势识别器,使用 initWithTarget:action: 并且我知道如何设置它的属性,我不知道如何让这个手势做一些事情.我假设它是通过操作参数 @selector 但这似乎永远不会被调用.我这样做错了吗?这是我所拥有的:

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];

对于我有的选择器:

-(void)animateSwipeRightLeft {

    //do stuff...
}

所以归结为几个相关问题:我是否正确设置了它?如果是这样,为什么我的选择器没有被调用?如果我错误地认为 IB 无法做到这一点,该怎么办?

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?

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

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

推荐答案

您应该在 SKScenedidMoveToView 方法中添加手势识别器.

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.viewnil 内的 init 方法

这篇关于如何向skView添加手势?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆