Swift Xcode 中的游乐场 - UILabel 的 TapGestureRecognizer 不工作
Swift Playground in Xcode - TapGestureRecognizer to UILabel not working
我正在 Xcode 中创建一个 Swift 游乐场,我在将 TapGestureRecognizer 放入 UILabel 时遇到问题...
class MyViewController : UIViewController {
override func loadView() {
func tapped() {
print("The label was tapped!")
}
let label11 = UILabel()
label11.frame = CGRect(x: -350, y: 360, width: 350, height: 20)
label11.text = "name."
label11.textColor = .black
label11.isUserInteractionEnabled = true
view.addSubview(label11)
let tap = UITapGestureRecognizer(target: self, action: "tapped")
label11.addGestureRecognizer(tap)
}
}
看来您正在以编程方式创建视图控制器,因为您仅在 loadView
中操作(您正确地没有调用 super
),这需要您创建实际的 view
那个控制器。
import UIKit
import PlaygroundSupport
class MyViewController: UIViewController {
override func loadView() {
view = UIView()
let label11 = UILabel()
label11.frame = CGRect(x: 0, y: 360, width: 350, height: 20)
label11.text = "name."
label11.textColor = .yellow
label11.isUserInteractionEnabled = true
view.addSubview(label11)
let tap = UITapGestureRecognizer(target: self, action: #selector(tapped))
label11.addGestureRecognizer(tap)
}
@objc func tapped() {
print("The label was tapped!")
}
}
PlaygroundPage.current.liveView = MyViewController()
或者,您可以使用 viewDidLoad
(这需要您调用 super)来复制 non-programmatic 视图控制器。
import UIKit
import PlaygroundSupport
class MyViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let label11 = UILabel()
label11.frame = CGRect(x: 0, y: 360, width: 350, height: 20)
label11.text = "name."
label11.textColor = .yellow
label11.isUserInteractionEnabled = true
view.addSubview(label11)
let tap = UITapGestureRecognizer(target: self, action: #selector(tapped))
label11.addGestureRecognizer(tap)
}
@objc func tapped() {
print("The label was tapped!")
}
}
PlaygroundPage.current.liveView = MyViewController()
此外,您的 origin-x
值超出范围,这就是为什么您可能没有看到标签并且您缺少 Swift 4 中要求的 @objc
语法的原因。 =18=]
我正在 Xcode 中创建一个 Swift 游乐场,我在将 TapGestureRecognizer 放入 UILabel 时遇到问题...
class MyViewController : UIViewController {
override func loadView() {
func tapped() {
print("The label was tapped!")
}
let label11 = UILabel()
label11.frame = CGRect(x: -350, y: 360, width: 350, height: 20)
label11.text = "name."
label11.textColor = .black
label11.isUserInteractionEnabled = true
view.addSubview(label11)
let tap = UITapGestureRecognizer(target: self, action: "tapped")
label11.addGestureRecognizer(tap)
}
}
看来您正在以编程方式创建视图控制器,因为您仅在 loadView
中操作(您正确地没有调用 super
),这需要您创建实际的 view
那个控制器。
import UIKit
import PlaygroundSupport
class MyViewController: UIViewController {
override func loadView() {
view = UIView()
let label11 = UILabel()
label11.frame = CGRect(x: 0, y: 360, width: 350, height: 20)
label11.text = "name."
label11.textColor = .yellow
label11.isUserInteractionEnabled = true
view.addSubview(label11)
let tap = UITapGestureRecognizer(target: self, action: #selector(tapped))
label11.addGestureRecognizer(tap)
}
@objc func tapped() {
print("The label was tapped!")
}
}
PlaygroundPage.current.liveView = MyViewController()
或者,您可以使用 viewDidLoad
(这需要您调用 super)来复制 non-programmatic 视图控制器。
import UIKit
import PlaygroundSupport
class MyViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let label11 = UILabel()
label11.frame = CGRect(x: 0, y: 360, width: 350, height: 20)
label11.text = "name."
label11.textColor = .yellow
label11.isUserInteractionEnabled = true
view.addSubview(label11)
let tap = UITapGestureRecognizer(target: self, action: #selector(tapped))
label11.addGestureRecognizer(tap)
}
@objc func tapped() {
print("The label was tapped!")
}
}
PlaygroundPage.current.liveView = MyViewController()
此外,您的 origin-x
值超出范围,这就是为什么您可能没有看到标签并且您缺少 Swift 4 中要求的 @objc
语法的原因。 =18=]