本文介绍了在 Swift 中将 TapGestureRecognizer 添加到 UILabel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我添加了一个 UITapGestureRecognizer:tapGesture
,我希望当我点击(触摸)标签时,klikPlay()
会运行它的动作.
I add a UITapGestureRecognizer:tapGesture
, I want that when I click(touch) the label, klikPlay()
will run its actions.
当我运行代码并单击/触摸标签时,会出现以下错误:
when I run the code and click/touch the label, it gives this error:
无法识别的选择器发送到实例.
是关于标签Player(named: label)
、UITapGestureRecognizer(named:tapGesture)
和函数klikPlay
我做错了什么?
override func didMoveToView(view: SKView) {
let background = SKSpriteNode(imageNamed: "bgStart2")
background.position = CGPoint(x:0, y:0)
background.anchorPoint=CGPoint(x:0,y:1.0)
background.size = frame.size
addChild(background)
//label Play
var label: UILabel = UILabel()
label.frame = CGRectMake(frame.size.width/4, frame.size.height/9, frame.size.width, frame.size.height/6)
label.text = "Play"
label.font = UIFont(name: "Noteworthy-Bold", size:50)
label.textColor = UIColor.whiteColor()
label.textAlignment = NSTextAlignment.Center
label.center = CGPointMake(frame.size.width/2, 4 * frame.size.height/8 )
label.hidden = false
//label map
var label2: UILabel = UILabel()
label2.frame = CGRectMake(frame.size.width/4, frame.size.height/9, frame.size.width, frame.size.height/6)
label2.text = "Map"
label2.font = UIFont(name: "Noteworthy-Bold", size:50)
label2.textColor = UIColor.whiteColor()
label2.textAlignment = NSTextAlignment.Center
label2.center = CGPointMake(frame.size.width/2, 5 * frame.size.height/8 )
label2.hidden = false
//label sounds
var label3: UILabel = UILabel()
label3.frame = CGRectMake(frame.size.width/4, frame.size.height/9, frame.size.width, frame.size.height/6)
label3.text = "Sounds"
label3.font = UIFont(name: "Noteworthy-Bold", size:50)
label3.textColor = UIColor.whiteColor()
label3.textAlignment = NSTextAlignment.Center
label3.center = CGPointMake(frame.size.width/2, 6 * frame.size.height/8 )
label3.hidden = false
//label info
var label4: UILabel = UILabel()
label4.frame = CGRectMake(frame.size.width/4, frame.size.height/9, frame.size.width, frame.size.height/6)
label4.text = "Info"
label4.font = UIFont(name: "Noteworthy-Bold", size:50)
label4.textColor = UIColor.whiteColor()
label4.textAlignment = NSTextAlignment.Center
label4.center = CGPointMake(frame.size.width/2, 7 * frame.size.height/8 )
label4.hidden = false
self.view?.addSubview(label)
self.view?.addSubview(label2)
self.view?.addSubview(label3)
self.view?.addSubview(label4)
label.userInteractionEnabled = true
label2.userInteractionEnabled = true
label3.userInteractionEnabled = true
label4.userInteractionEnabled = true
//the UITapGestureRecognizer
let tapGesture = UITapGestureRecognizer(target: self, action: "klikPlay:")
label.addGestureRecognizer(tapGesture)
view.addGestureRecognizer(tapGesture)
//the functions I want to run
func klikPlay(sender:UITapGestureRecognizer){
var scene:GameScene!
self.runAction(SKAction.sequence([SKAction.waitForDuration(0.0),
SKAction.runBlock({
// var transition:SKTransition = SKTransition.flipHorizontalWithDuration(0.5)
var scene1:SKScene = GameScene(size: self.size)
self.view?.presentScene(scene1)
label.removeFromSuperview()
label2.removeFromSuperview()
label3.removeFromSuperview()
label4.removeFromSuperview()
background.removeFromParent()
})
] ))
}
}
推荐答案
看起来您已经将 klikPlay
声明为 didMoveToView
中的嵌套函数:
It looks like you've declared klikPlay
as a nested function inside didMoveToView
:
override func didMoveToView(view: SKView) {
// ...
let tapGesture = UITapGestureRecognizer(target: self, action: "klikPlay:")
func klikPlay(sender:UITapGestureRecognizer){
// ...
}
}
您需要它作为类的实例方法,以便手势识别器正确找到它:
You need it as an instance method of your class for the gesture recognizer to find it properly:
class MyView {
var label: UILabel!
// ...
override func didMoveToView(view: SKView) {
label = UILabel()
label.frame = CGRectMake(...)
// ...
let tapGesture = UITapGestureRecognizer(target: self, action: "klikPlay:")
}
func klikPlay(sender:UITapGestureRecognizer){
// ...
}
}
这篇关于在 Swift 中将 TapGestureRecognizer 添加到 UILabel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!