Swift 4 - UITapGestureRecognizer 选择器函数未调用
Swift 4 - UITapGestureRecognizer selector function not calling
我正在尝试将 "click event" 添加到标签而不是按钮。我没有使用故事板。
[...]
let registerLabel: UILabel = {
let label = UILabel()
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(openLoginView))
tapGesture.numberOfTapsRequired = 1
label.translatesAutoresizingMaskIntoConstraints = false
label.text = "Register"
label.isUserInteractionEnabled = true
label.addGestureRecognizer(tapGesture)
label.textColor = Colors.lightGrey
label.font = label.font.withSize(18)
return label
}()
[...] // viewDidLoad()
@objc private func openLoginView(sender: UITapGestureRecognizer) {
print("PRINT ME-->>>>>>>>>")
}
[...]
在 iPhone 6s 模拟器中,当我使用鼠标模拟点击鼠标点击时,控制台内部没有打印任何内容。我错过了什么吗?
您需要延迟声明 registerLabel
。像这样:
lazy var registerLabel: UILabel = {
let label = UILabel(frame: CGRect(x: 200, y: 300, width: 200, height: 50))
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(openLoginView(sender:)))
tapGesture.numberOfTapsRequired = 1
label.translatesAutoresizingMaskIntoConstraints = false
label.text = "Register"
label.isUserInteractionEnabled = true
label.addGestureRecognizer(tapGesture)
label.textColor = .black
label.font = label.font.withSize(18)
return label
}()
此外,如果您需要对 openLoginView
函数中的 sender
执行某些操作,请查看我的选择器。
有关惰性存储属性的更多信息
A lazy stored property is a property whose initial value is not
calculated until the first time it is used. You indicate a lazy stored
property by writing the lazy modifier before its declaration.
https://docs.swift.org/swift-book/LanguageGuide/Properties.html
希望对您有所帮助! :)
我正在尝试将 "click event" 添加到标签而不是按钮。我没有使用故事板。
[...]
let registerLabel: UILabel = {
let label = UILabel()
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(openLoginView))
tapGesture.numberOfTapsRequired = 1
label.translatesAutoresizingMaskIntoConstraints = false
label.text = "Register"
label.isUserInteractionEnabled = true
label.addGestureRecognizer(tapGesture)
label.textColor = Colors.lightGrey
label.font = label.font.withSize(18)
return label
}()
[...] // viewDidLoad()
@objc private func openLoginView(sender: UITapGestureRecognizer) {
print("PRINT ME-->>>>>>>>>")
}
[...]
在 iPhone 6s 模拟器中,当我使用鼠标模拟点击鼠标点击时,控制台内部没有打印任何内容。我错过了什么吗?
您需要延迟声明 registerLabel
。像这样:
lazy var registerLabel: UILabel = {
let label = UILabel(frame: CGRect(x: 200, y: 300, width: 200, height: 50))
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(openLoginView(sender:)))
tapGesture.numberOfTapsRequired = 1
label.translatesAutoresizingMaskIntoConstraints = false
label.text = "Register"
label.isUserInteractionEnabled = true
label.addGestureRecognizer(tapGesture)
label.textColor = .black
label.font = label.font.withSize(18)
return label
}()
此外,如果您需要对 openLoginView
函数中的 sender
执行某些操作,请查看我的选择器。
有关惰性存储属性的更多信息
A lazy stored property is a property whose initial value is not calculated until the first time it is used. You indicate a lazy stored property by writing the lazy modifier before its declaration.
https://docs.swift.org/swift-book/LanguageGuide/Properties.html
希望对您有所帮助! :)