Adding TapGestureRecognizer to UILabel in Swift

2020-08-26 03:29发布

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:

unrecognized selector sent to instance.

It is about the label Player(named: label), the UITapGestureRecognizer(named:tapGesture) and the function klikPlay

What am I doing wrong?

 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条回答
走好不送
2楼-- · 2020-08-26 03:57

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){
        // ...
    }
}
查看更多
3楼-- · 2020-08-26 04:02

Based on answer from Taimur Ajmal, Updated for 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)")
}
查看更多
别忘想泡老子
4楼-- · 2020-08-26 04:13

Pretty straight forward

let tap:UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "labelAction:")
mylabel.addGestureRecognizer(tap)
tap.delegate = self // Remember to extend your class with UIGestureRecognizerDelegate

 // Receive action
func labelAction(gr:UITapGestureRecognizer)
{
    let searchlbl:UILabel = (gr.view as! UILabel) // Type cast it with the class for which you have added gesture
    print(searchlbl.text)    
}
查看更多
登录 后发表回答