在 UILabel 中使用多个点击手势
using multiple tap gestures with UILabel
我正在创建一个 UILabel
需要有两种手势,单击和双击。
class ViewController: UIViewController, UIGestureRecognizerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(labelView)
labelView.topAnchor.constraint(equalTo: view.topAnchor),
quoteLabelView.leadingAnchor.constraint(equalTo:
view.leadingAnchor),
quoteLabelView.trailingAnchor.constraint(equalTo:
view.trailingAnchor),
quoteLabelView.bottomAnchor.constraint(equalTo:
view.topAnchor),
}
var labelView: UILabel = {
let label = UILabel()
label.translatesAutoresizingMaskIntoConstraints = false
label.isUserInteractionEnabled = true
let singleTapGesture = UITapGestureRecognizer(target: self, action: #selector(handleSingleTap))
let doubleTapGesture = UITapGestureRecognizer(target: self, action: #selector(handleDoubleTap))
singleTapGesture.numberOfTapsRequired = 1
doubleTapGesture.numberOfTapsRequired = 2
label.addGestureRecognizer(singleTapGesture)
label.addGestureRecognizer(doubleTapGesture)
return label
}()
}
我阅读了 apples 文档,其中提供了有关优先选择一种手势而不是另一种手势的信息,但我不知道如何将其应用于标签。
https://developer.apple.com/documentation/uikit/touches_presses_and_gestures/coordinating_multiple_gesture_recognizers/preferring_one_gesture_over_another
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer,
shouldRequireFailureOf otherGestureRecognizer: UIGestureRecognizer) -> Bool {
// Don't recognize a single tap until a double-tap fails.
if gestureRecognizer == self.tapGesture &&
otherGestureRecognizer == self.doubleTapGesture {
return true
}
return false
}
有什么想法吗?
如果您想在标签声明中添加手势识别器,最好为单击和双击使用单独的函数:
class TestViewController: UIViewController {
lazy var labelView: UILabel = {
let label = UILabel()
label.translatesAutoresizingMaskIntoConstraints = false
label.isUserInteractionEnabled = true
let singleTapGesture = UITapGestureRecognizer(target: self, action: #selector(didSingleTap(_:)))
singleTapGesture.numberOfTapsRequired = 1
label.addGestureRecognizer(singleTapGesture)
let doubleTapGesture = UITapGestureRecognizer(target: self, action: #selector(didDoubleTap(_:)))
doubleTapGesture.numberOfTapsRequired = 2
label.addGestureRecognizer(doubleTapGesture)
singleTapGesture.require(toFail: doubleTapGesture)
return label
}()
override func viewDidLoad() {
super.viewDidLoad()
// add the labelView
view.addSubview(labelView)
// set a couple properties
labelView.text = "Tap or Double-Tap Me"
labelView.textAlignment = .center
labelView.backgroundColor = .yellow
// constrain it to the view
NSLayoutConstraint.activate([
labelView.topAnchor.constraint(equalTo: view.topAnchor, constant: 200),
labelView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
labelView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
])
}
@objc func didSingleTap(_ gesture: UITapGestureRecognizer) -> Void {
print("Did Single Tap")
}
@objc func didDoubleTap(_ gesture: UITapGestureRecognizer) -> Void {
print("Did Double Tap")
}
}
正如您分享的 link 所说,您需要为手势识别器设置委托:
class ViewController: UIViewController, UIGestureRecognizerDelegate {
var singleTapGesture = UITapGestureRecognizer(target: self, action: #selector(handleSingleTap))
var doubleTapGesture = UITapGestureRecognizer(target: self, action: #selector(handleDoubleTap))
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(labelView)
setupGestures()
labelView.topAnchor.constraint(equalTo: view.topAnchor),
quoteLabelView.leadingAnchor.constraint(equalTo:
view.leadingAnchor),
quoteLabelView.trailingAnchor.constraint(equalTo:
view.trailingAnchor),
quoteLabelView.bottomAnchor.constraint(equalTo:
view.topAnchor),
}
func setupGestures() {
singleTapGesture.numberOfTapsRequired = 1
singleTapGesture.delegate = self
doubleTapGesture.numberOfTapsRequired = 2
doubleTapGesture.delegate = self
labelView.addGestureRecognizer(singleTapGesture)
labelView.addGestureRecognizer(doubleTapGesture)
}
var labelView: UILabel = {
let label = UILabel()
label.translatesAutoresizingMaskIntoConstraints = false
label.isUserInteractionEnabled = true
return label
}()
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer,
shouldRequireFailureOf otherGestureRecognizer: UIGestureRecognizer) -> Bool {
// Don't recognize a single tap until a double-tap fails.
if gestureRecognizer == self.tapGesture &&
otherGestureRecognizer == self.doubleTapGesture {
return true
}
return false
}
}
然后是你发的方法
此外,您需要能够访问手势,因此请将其从标签中删除。
我正在创建一个 UILabel
需要有两种手势,单击和双击。
class ViewController: UIViewController, UIGestureRecognizerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(labelView)
labelView.topAnchor.constraint(equalTo: view.topAnchor),
quoteLabelView.leadingAnchor.constraint(equalTo:
view.leadingAnchor),
quoteLabelView.trailingAnchor.constraint(equalTo:
view.trailingAnchor),
quoteLabelView.bottomAnchor.constraint(equalTo:
view.topAnchor),
}
var labelView: UILabel = {
let label = UILabel()
label.translatesAutoresizingMaskIntoConstraints = false
label.isUserInteractionEnabled = true
let singleTapGesture = UITapGestureRecognizer(target: self, action: #selector(handleSingleTap))
let doubleTapGesture = UITapGestureRecognizer(target: self, action: #selector(handleDoubleTap))
singleTapGesture.numberOfTapsRequired = 1
doubleTapGesture.numberOfTapsRequired = 2
label.addGestureRecognizer(singleTapGesture)
label.addGestureRecognizer(doubleTapGesture)
return label
}()
}
我阅读了 apples 文档,其中提供了有关优先选择一种手势而不是另一种手势的信息,但我不知道如何将其应用于标签。 https://developer.apple.com/documentation/uikit/touches_presses_and_gestures/coordinating_multiple_gesture_recognizers/preferring_one_gesture_over_another
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer,
shouldRequireFailureOf otherGestureRecognizer: UIGestureRecognizer) -> Bool {
// Don't recognize a single tap until a double-tap fails.
if gestureRecognizer == self.tapGesture &&
otherGestureRecognizer == self.doubleTapGesture {
return true
}
return false
}
有什么想法吗?
如果您想在标签声明中添加手势识别器,最好为单击和双击使用单独的函数:
class TestViewController: UIViewController {
lazy var labelView: UILabel = {
let label = UILabel()
label.translatesAutoresizingMaskIntoConstraints = false
label.isUserInteractionEnabled = true
let singleTapGesture = UITapGestureRecognizer(target: self, action: #selector(didSingleTap(_:)))
singleTapGesture.numberOfTapsRequired = 1
label.addGestureRecognizer(singleTapGesture)
let doubleTapGesture = UITapGestureRecognizer(target: self, action: #selector(didDoubleTap(_:)))
doubleTapGesture.numberOfTapsRequired = 2
label.addGestureRecognizer(doubleTapGesture)
singleTapGesture.require(toFail: doubleTapGesture)
return label
}()
override func viewDidLoad() {
super.viewDidLoad()
// add the labelView
view.addSubview(labelView)
// set a couple properties
labelView.text = "Tap or Double-Tap Me"
labelView.textAlignment = .center
labelView.backgroundColor = .yellow
// constrain it to the view
NSLayoutConstraint.activate([
labelView.topAnchor.constraint(equalTo: view.topAnchor, constant: 200),
labelView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
labelView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
])
}
@objc func didSingleTap(_ gesture: UITapGestureRecognizer) -> Void {
print("Did Single Tap")
}
@objc func didDoubleTap(_ gesture: UITapGestureRecognizer) -> Void {
print("Did Double Tap")
}
}
正如您分享的 link 所说,您需要为手势识别器设置委托:
class ViewController: UIViewController, UIGestureRecognizerDelegate {
var singleTapGesture = UITapGestureRecognizer(target: self, action: #selector(handleSingleTap))
var doubleTapGesture = UITapGestureRecognizer(target: self, action: #selector(handleDoubleTap))
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(labelView)
setupGestures()
labelView.topAnchor.constraint(equalTo: view.topAnchor),
quoteLabelView.leadingAnchor.constraint(equalTo:
view.leadingAnchor),
quoteLabelView.trailingAnchor.constraint(equalTo:
view.trailingAnchor),
quoteLabelView.bottomAnchor.constraint(equalTo:
view.topAnchor),
}
func setupGestures() {
singleTapGesture.numberOfTapsRequired = 1
singleTapGesture.delegate = self
doubleTapGesture.numberOfTapsRequired = 2
doubleTapGesture.delegate = self
labelView.addGestureRecognizer(singleTapGesture)
labelView.addGestureRecognizer(doubleTapGesture)
}
var labelView: UILabel = {
let label = UILabel()
label.translatesAutoresizingMaskIntoConstraints = false
label.isUserInteractionEnabled = true
return label
}()
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer,
shouldRequireFailureOf otherGestureRecognizer: UIGestureRecognizer) -> Bool {
// Don't recognize a single tap until a double-tap fails.
if gestureRecognizer == self.tapGesture &&
otherGestureRecognizer == self.doubleTapGesture {
return true
}
return false
}
}
然后是你发的方法
此外,您需要能够访问手势,因此请将其从标签中删除。