如何使 UILabel 可点击?

How to make a UILabel clickable?

我想让 UILabel 可以点击。

我已经试过了,但是没用:

class DetailViewController: UIViewController {

    @IBOutlet weak var tripDetails: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()
        ...
        let tap = UITapGestureRecognizer(target: self, action: Selector("tapFunction:"))
        tripDetails.addGestureRecognizer(tap)
    }

    func tapFunction(sender:UITapGestureRecognizer) {
        print("tap working")
    }
}

您是否尝试过将 tripDetails 标签上的 isUserInteractionEnabled 设置为 true?这应该有效。

您需要启用该标签的用户交互......

例如

yourLabel.userInteractionEnabled = true

Swift 3 次更新

yourLabel.isUserInteractionEnabled = true

Swift 3 次更新

替换

Selector("tapFunction:")

#selector(DetailViewController.tapFunction)

示例:

class DetailViewController: UIViewController {

    @IBOutlet weak var tripDetails: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()
        ...

        let tap = UITapGestureRecognizer(target: self, action: #selector(DetailViewController.tapFunction))
        tripDetails.isUserInteractionEnabled = true
        tripDetails.addGestureRecognizer(tap)
    }

    @objc
    func tapFunction(sender:UITapGestureRecognizer) {
        print("tap working")
    }
}

对于swift3.0还可以更改手势长按持续时间

label.isUserInteractionEnabled = true
let longPress:UILongPressGestureRecognizer = UILongPressGestureRecognizer.init(target: self, action: #selector(userDragged(gesture:))) 
longPress.minimumPressDuration = 0.2
label.addGestureRecognizer(longPress)

SWIFT 4 次更新

 @IBOutlet weak var tripDetails: UILabel!

 override func viewDidLoad() {
    super.viewDidLoad()

    let tap = UITapGestureRecognizer(target: self, action: #selector(GameViewController.tapFunction))
    tripDetails.isUserInteractionEnabled = true
    tripDetails.addGestureRecognizer(tap)
}

@objc func tapFunction(sender:UITapGestureRecognizer) {

    print("tap working")
}

如上述解决方案所述 您应该首先启用用户交互并添加点击手势

此代码已使用

进行测试

Swift4 - Xcode 9.2

yourlabel.isUserInteractionEnabled = true
yourlabel.addGestureRecognizer(UITapGestureRecognizer(){
                //TODO 
            })

好又方便的解决方案:

在你的ViewController中:

@IBOutlet weak var label: LabelButton!

override func viewDidLoad() {
    super.viewDidLoad()

    self.label.onClick = {
        // TODO
    }
}

您可以将它放在您的 ViewController 或另一个 .swift 文件中(例如 CustomView.swift):

@IBDesignable class LabelButton: UILabel {
    var onClick: () -> Void = {}
    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        onClick()
    }
}

在 Storyboard select Label 和右窗格中 "Identity Inspector" 字段 class select LabelButton。

不要忘记在标签属性检查器中启用"User Interaction Enabled"

Swift 5

类似于@liorco,但需要将@objc替换为@IBAction.

class DetailViewController: UIViewController {

    @IBOutlet weak var tripDetails: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()
        ...

        let tap = UITapGestureRecognizer(target: self, action: #selector(DetailViewController.tapFunction))
        tripDetails.isUserInteractionEnabled = true
        tripDetails.addGestureRecognizer(tap)
    }

    @IBAction func tapFunction(sender: UITapGestureRecognizer) {
        print("tap working")
    }
}

这正在 Xcode 10.2.

像我一样很容易被忽视,但不要忘记使用 UITapGestureRecognizer 而不是 UIGestureRecognizer

谢谢researcher

这是我使用 UIKit 的编程用户界面解决方案。

我只在 Swift 5 上试过。它奏效了。

有趣的是,您不必明确设置 isUserInteractionEnabled = true

import UIKit

open class LabelButon: UILabel {
    var onClick: () -> Void = {}
    
    public override init(frame: CGRect) {
        super.init(frame: frame)
        isUserInteractionEnabled = true
    }
    
    public required init?(coder: NSCoder) {
        super.init(coder: coder)
    }
    
    public convenience init() {
        self.init(frame: .zero)
    }
    
    open override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        onClick()
    }
}

使用:

override func viewDidLoad() {
    super.viewDidLoad()
    
    let label = LabelButton()
    label.text = "Label"
    label.onClick = {
        // TODO
    }
}

Don't forget to set constraints. Otherwise it won't appear on view.