ios 如何使 UILabel 可点击?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33658521/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 08:13:09  来源:igfitidea点击:

How to make a UILabel clickable?

iosswiftuilabeluitapgesturerecognizer

提问by Daniele B

I would like to make a UILabel clickable.

我想让 UILabel 可点击。

I have tried this, but it doesn't work:

我试过这个,但它不起作用:

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

回答by Claudiu Iordache

Have you tried to set isUserInteractionEnabledto trueon the tripDetailslabel? This should work.

您是否尝试在标签上设置isUserInteractionEnabled为?这应该有效。truetripDetails

回答by liorco

Swift 3 Update

斯威夫特 3 更新

Replace

代替

Selector("tapFunction:")

with

#selector(DetailViewController.tapFunction)

Example:

例子:

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

回答by Xcodian Solangi

SWIFT 4 Update

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

回答by Indrajit Sinh Rayjada

Swift 3 Update

斯威夫特 3 更新

yourLabel.isUserInteractionEnabled = true

回答by Jerry Chong

Swift 5

斯威夫特 5

Similar to @liorco, but need to replace @objcwith @IBAction.

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

This is working on Xcode 10.2.

这适用于 Xcode 10.2。

回答by researcher

Good and convenient solution:

好用又方便的解决方法:

In your ViewController:

在您的 ViewController 中:

@IBOutlet weak var label: LabelButton!

override func viewDidLoad() {
    super.viewDidLoad()

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

You can place this in your ViewController or in another .swift file(e.g. CustomView.swift):

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

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

In Storyboard select Label and on right pane in "Identity Inspector" in field class select LabelButton.

在 Storyboard 中选择 Label,然后在字段类中的“Identity Inspector”的右侧窗格中选择 LabelButton。

Don't forget to enable in Label Attribute Inspector "User Interaction Enabled"

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

回答by Abhishek

You need to enable the user interaction of that label.....

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

For e.g

例如

yourLabel.userInteractionEnabled = true

yourLabel.userInteractionEnabled = true

回答by DURGESH

For swift 3.0 You can also change gesture long press time duration

对于 swift 3.0 您还可以更改手势长按持续时间

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

回答by Devbot10

Pretty easy to overlook like I did, but don't forget to use UITapGestureRecognizerrather than UIGestureRecognizer.

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

回答by Amr Angry

As described in the above solution you should enable the user interaction first and add the tap gesture

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

this code has been tested using

此代码已使用测试

Swift4 - Xcode 9.2

Swift4 - Xcode 9.2

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