在Swift中将TapGestureRecognizer添加到UILabel

时间:2014-11-21 15:24:11

标签: swift uilabel touch-event

我添加UITapGestureRecognizer:tapGesture,我希望当我点击(触摸)标签时,klikPlay()将会执行其操作。

当我运行代码并单击/触摸标签时,它会出现此错误:

  

无法识别的选择器发送到实例。

关于标签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()
        })
        ] ))
}
}

3 个答案:

答案 0 :(得分:14)

基于answer from Taimur Ajmal,更新为 Swift 2.X

override func viewDidLoad() {
    super.viewDidLoad()

    ...

    labelDemo.userInteractionEnabled = true // Remember to do this
    let tap: UITapGestureRecognizer = UITapGestureRecognizer(
        target: self, action: #selector(didTapLabelDemo))
    labelDemo.addGestureRecognizer(tap)
    tap.delegate = self // Remember to extend your class with UIGestureRecognizerDelegate

    ...
}

func didTapLabelDemo(sender: UITapGestureRecognizer)
{
    print("you tapped label \(sender)")
}

答案 1 :(得分:12)

非常直接

SELECT (ABS(field) + field)/2 FROM Table

答案 2 :(得分:6)

您似乎已将klikPlay声明为didMoveToView内的嵌套函数:

override func didMoveToView(view: SKView) {
    // ...
    let tapGesture = UITapGestureRecognizer(target: self, action: "klikPlay:")

    func klikPlay(sender:UITapGestureRecognizer){
        // ...
    }
}

您需要将它作为类的实例方法,以便手势识别器正确找到它:

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){
        // ...
    }
}