我想点击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")
}
}
答案 0 :(得分:144)
您是否尝试在isUserInteractionEnabled
标签上将true
设为tripDetails
?这应该有用。
答案 1 :(得分:90)
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")
}
}
答案 2 :(得分:38)
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")
}
答案 3 :(得分:13)
Swift 3更新
yourLabel.isUserInteractionEnabled = true
答案 4 :(得分:3)
您需要启用该标签的用户互动.....
例如
yourLabel.userInteractionEnabled = true
答案 5 :(得分: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)
答案 6 :(得分:2)
好方便的解决方案:
在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 Inspector”的右窗格中选择LabelButton。
不要忘记在Label Attribute Inspector中启用“启用用户交互”
答案 7 :(得分:2)
这是我使用 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
}
}
<块引用>
不要忘记设置约束。否则它不会出现在视图中。
答案 8 :(得分:1)
快速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。
答案 9 :(得分:0)
像我一样容易忽略,但不要忘记使用UITapGestureRecognizer
而不是UIGestureRecognizer
。
答案 10 :(得分:-4)
如上述解决方案中所述 您应首先启用用户互动并添加点按手势
此代码已使用
进行测试yourlabel.isUserInteractionEnabled = true
yourlabel.addGestureRecognizer(UITapGestureRecognizer(){
//TODO
})