简体   繁体   English

SpriteKit和UISwipeGestureRecognizer

[英]SpriteKit and UISwipeGestureRecognizer

In my viewDidLoad function, I setup a swipe gesture recognizer: 在我的viewDidLoad函数中,我设置了一个滑动手势识别器:

var swipeRecognizer:UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: Selector("move"))
swipeRecognizer.direction = .Right
view.addGestureRecognizer(swipeRecognizer)

And then I set up the move function: 然后我设置了移动功能:

func move(swipe:UISwipeGestureRecognizer) {
    NSLog("swiped")
}

However, I keep getting the following error when I swipe right: 但是,当我向右滑动时,我不断收到以下错误:

[_TtC8swiftris9GameScene move]: unrecognized selector sent to instance 0xc81c200
2014-06-03 14:52:57.560 swiftris[45440:6777826] 
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', 
reason: '-[_TtC8swiftris9GameScene move]: unrecognized selector sent to instance 0xc81c200'

What could be the problem? 可能是什么问题呢?

You defined your function as move(swipe:UISwipeGestureRecognizer) , which maps to the obj-c method name move: , but your selector is just "move" . 您将函数定义为move(swipe:UISwipeGestureRecognizer) ,它映射到obj-c方法名称move: ,但您的选择器只是"move" You need to use "move:" instead. 你需要使用"move:"代替。

As @Kevin Ballard correctly points out, your selector doesn't match your method, which explains the "unrecognized selector" exception. 正如@Kevin Ballard正确指出的那样,你的选择器与你的方法不匹配,这解释了“无法识别的选择器”异常。 However, I think it's worth noting that you can ditch the cast to Selector altogether, and use a string literal in its stead. 但是,我认为值得注意的是,您可以完全抛弃转换为Selector,并使用字符串文字代替它。

Quote from 引用自

You can construct a selector with a string literal, such as let mySelector: Selector = "tappedButton:". 您可以使用字符串文字构造一个选择器,例如让mySelector:Selector =“tappedButton:”。 Because string literals can be automatically converted to selectors, you can pass a string literal to any method that accepts a selector. 因为字符串文字可以自动转换为选择器,所以您可以将字符串文字传递给任何接受选择器的方法。

Example: 例:

let gesture: UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action:"move:")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM