如何使UILabel可点击?


136

我想使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")
    }
}

4
你的身材是什么UILabel?您确定要触摸标签框吗?你有UIViews覆盖标签吗?被userInteractionEnabled设置True为标签?
2015年

确保您的UILabel IBOutlet已从笔尖或情节提要板中挂接
aahrens 2015年

Answers:


178

你有没有尝试设置isUserInteractionEnabledtruetripDetails标签?这应该工作。


1
感谢你的回答!!!在这里,我还有另一个关于UITapGestureRecognizer的问题:stackoverflow.com/questions/33659078/…–
Daniele B

我正在尝试在点击时更改背景颜色,但是当我松开手指时会触发点击事件。有没有办法赶上达阵事件?长按不是我想要的。
努曼·卡拉阿斯兰

109

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")
    }
}

我必须用注释一下tap功能@objc
Scott D. Strader

46

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")
}


14

斯威夫特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。


2
@IBAction仅需要向Xcode的Interface Builder(因此IB)公开方法即可。它也可以用作代码,@objc但是如果您使用代码添加手势或其他动作,则应使用@objc注释
亚当

7

良好便捷的解决方案:

在您的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()
    }
}

在情节提要中选择“标签”,然后在字段类的“身份检查器”的右窗格中,选择“ LabelButton”。

不要忘记在标签属性检查器中启用“已启用用户交互”


3

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

例如

yourLabel.userInteractionEnabled = true


这对我没有帮助。我正在尝试在表格视图中的标签上执行此操作
Pavlos

2

对于Swift 3.0,您还可以更改手势长按时间

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

1

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


-4

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

此代码已通过测试

Swift4-Xcode 9.2

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

Cannot invoke initializer for type 'UITapGestureRecognizer' with an argument list of type '(() -> Void)'当我尝试这个时,我明白了。
lionello
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.