gpt4 book ai didi

ios - 如果快速触发按钮上的长按手势,如何获取按钮文本?

转载 作者:行者123 更新时间:2023-12-01 21:59:47 25 4
gpt4 key购买 nike

我定义了一个按钮,并且长按手势识别器已添加到该按钮。我知道可以通过定义如下函数来完成某些操作。但是,当检测到手势时,如何获取与按钮关联的标签文本?

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")
}

}

最佳答案


@objc func clearBottom(sender: UILongPressGestureRecognizer) { 
if sender.state == .ended {
// how do I get the button text here? (i.e. "Item Name AAA")
let button = sender.view as! UIButton
print(button.titleLabel?.text)
}
}

关于ios - 如果快速触发按钮上的长按手势,如何获取按钮文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60346520/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com