如何使 UILabel 可点击?

我想使 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")
}
}
149088 次浏览

你试过在 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")
}
}

对于迅捷3.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 中选择 Label,在字段类中的“ Identity spector”的右窗格中选择 LabelButton。

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

Swift 5

与@liorco 类似,但需要用 @ IBAction代替 @ objecc

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

谢谢 研究员

下面是我使用 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
}
}

不要忘记设置约束,否则它不会出现在视图中。