我们如何将 UIGestureRecognizer 添加到 Outlet Collection?
How can we add a UIGestureRecognizer to Outlet Collection?
我正在尝试向出口标签集合 [UILabel] 添加点击手势,如下所示:
@IBOutlet var subLabels: [UILabel]!
override func viewDidLoad() {
super.viewDidLoad()
let tap = UITapGestureRecognizer(target: self, action: #selector(HomePageViewController.selectSubLabel(tap:)))
tap.numberOfTapsRequired = 1
tap.cancelsTouchesInView = false
for i in (0..<(subLabels.count)) {
subLabels[i].addGestureRecognizer(tap)
}
}
func selectSubLabel(tap: UITapGestureRecognizer) {
print("Gesture Is WORKING!")
}
我尝试将它添加到情节提要中的单个标签上;但是 NONE 正在工作。
请在 Xcode 的 Attribute inspector
中检查 UIlabel
的 User Interaction Enabled
属性。 User Interaction Enabled
必须勾选检测点击。请看下图,
首先,您需要允许用户在标签上进行交互(默认情况下处于关闭状态):
for i in (0..<(subLabels.count)) {
subLabels[i].isUserInteractionEnabled = true
subLabels[i].addGestureRecognizer(tap)
}
但手势识别器只能在一个视图中观察手势。
所以,有两个选择:
我。每个标签的专用手势识别器
for i in (0..<(labels.count)) {
let tap = UITapGestureRecognizer(target: self, action: #selector(selectSubLabel(tap:)))
labels[i].isUserInteractionEnabled = true
labels[i].addGestureRecognizer(tap)
}
二.标签父视图的一个手势识别器
override func viewDidLoad() {
super.viewDidLoad()
for i in (0..<(labels.count)) {
subLabels[i].isUserInteractionEnabled = true
}
let tap = UITapGestureRecognizer(target: self, action: #selector(selectSubLabel(tap:)))
view.addGestureRecognizer(tap)
}
func selectSubLabel(tap: UITapGestureRecognizer) {
let touchPoint = tap.location(in: view)
guard let label = subLabels.first(where: { [=12=].frame.contains(touchPoint) }) else { return }
// Do your stuff with the label
}
我正在尝试向出口标签集合 [UILabel] 添加点击手势,如下所示:
@IBOutlet var subLabels: [UILabel]!
override func viewDidLoad() {
super.viewDidLoad()
let tap = UITapGestureRecognizer(target: self, action: #selector(HomePageViewController.selectSubLabel(tap:)))
tap.numberOfTapsRequired = 1
tap.cancelsTouchesInView = false
for i in (0..<(subLabels.count)) {
subLabels[i].addGestureRecognizer(tap)
}
}
func selectSubLabel(tap: UITapGestureRecognizer) {
print("Gesture Is WORKING!")
}
我尝试将它添加到情节提要中的单个标签上;但是 NONE 正在工作。
请在 Xcode 的 Attribute inspector
中检查 UIlabel
的 User Interaction Enabled
属性。 User Interaction Enabled
必须勾选检测点击。请看下图,
首先,您需要允许用户在标签上进行交互(默认情况下处于关闭状态):
for i in (0..<(subLabels.count)) {
subLabels[i].isUserInteractionEnabled = true
subLabels[i].addGestureRecognizer(tap)
}
但手势识别器只能在一个视图中观察手势。 所以,有两个选择:
我。每个标签的专用手势识别器
for i in (0..<(labels.count)) {
let tap = UITapGestureRecognizer(target: self, action: #selector(selectSubLabel(tap:)))
labels[i].isUserInteractionEnabled = true
labels[i].addGestureRecognizer(tap)
}
二.标签父视图的一个手势识别器
override func viewDidLoad() {
super.viewDidLoad()
for i in (0..<(labels.count)) {
subLabels[i].isUserInteractionEnabled = true
}
let tap = UITapGestureRecognizer(target: self, action: #selector(selectSubLabel(tap:)))
view.addGestureRecognizer(tap)
}
func selectSubLabel(tap: UITapGestureRecognizer) {
let touchPoint = tap.location(in: view)
guard let label = subLabels.first(where: { [=12=].frame.contains(touchPoint) }) else { return }
// Do your stuff with the label
}