@IBOutlet weak var btn: UIButton!
override func viewDidLoad() {
let tapGesture = UITapGestureRecognizer(target: self, #selector (tap)) //Tap function will call when user tap on button
let longGesture = UILongPressGestureRecognizer(target: self, #selector(long)) //Long function will call when user long press on button.
tapGesture.numberOfTapsRequired = 1
btn.addGestureRecognizer(tapGesture)
btn.addGestureRecognizer(longGesture)
}
@objc func tap() {
print("Tap happend")
}
@objc func long() {
print("Long press")
}
//create your button using a factory (it'll be easier of course)
//For example you could have a variable in the custom class to have a unique identifier, or just use the tag property)
func createButtonWithInfo(buttonInfo: [String: Any]) -> CustomUIButton {
let button = UIButton(type: .custom)
button.tapDelegate = self
/*
Add gesture recognizers to the button as well as any other info in the buttonInfo
*/
return button
}
func buttonDelegateReceivedTapGestureRecognizerFrom(button: CustomUIButton){
//Whatever you want to do
}
3条答案
按热度按时间mkshixfv1#
如果你想执行任何操作与单击你和长按,你可以添加手势到按钮这样:
这样你就可以为单个按钮添加多个方法,你只需要为那个按钮选择插座。.
vhmi4jdf2#
7eumitmz3#
为什么不创建一个自定义的UIButton类,创建一个协议,让按钮将信息发送回delegte。就像这样: