У меня определена кнопка, и к ней был добавлен распознаватель жестов длительного нажатия. Я знаю, что некоторые действия могут быть выполнены путем определения функции, как показано ниже. Но как получить текст метки, связанный с кнопкой при обнаружении жеста?
var removeBottomButton: UIButton = UIButton()
// set up the button
override func viewDidLoad() {
super.viewDidLoad()
self.setRemoveButton()
let longGesture = UILongPressGestureRecognizer(target: self, action: #selector(clearBottom))
self.removeBottomButton.addGestureRecognizer(longGesture)
longGesture.minimumPressDuration = 1.5
}
func setRemoveButton() {
removeBottomButton = UIButton(type: UIButton.ButtonType.system)
removeBottomButton.translatesAutoresizingMaskIntoConstraints = false
removeBottomButton.setTitle("Item Name AAA", for: .normal)
removeBottomButton.titleLabel?.font = UIFont.boldSystemFont(ofSize: 20)
removeBottomButton.setTitleColor(UIColor.white, for: UIControl.State.normal)
removeBottomButton.backgroundColor = UIColor.black
removeBottomButton.layer.borderColor = UIColor.red.cgColor
removeBottomButton.layer.cornerRadius = 10.0
removeBottomButton.isUserInteractionEnabled = true
view.addSubview(removeBottomButton)
removeBottomButton.topAnchor.constraint(equalTo: view.bottomAnchor, constant: CGFloat(-50)).isActive = true
removeBottomButton.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: CGFloat(20)).isActive = true
removeBottomButton.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: CGFloat(-20)).isActive = true
removeBottomButton.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: CGFloat(-20)).isActive = true
}
//do something when the gesture is detected
@objc func clearBottom(sender: UILongPressGestureRecognizer) {
if sender.state == .ended {
// how do I get the button text here? (i.e. "Item Name AAA")
}
}