ios 带有单按和长按事件的 UIButton 迅速
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30859203/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UIButton with single press and long press events swift
提问by Kamal Upasena
I want to trigger two action on button click
and button long click
. I have add a UIbutton
in my interface builder. How can i trigger two action using IBAction
can somebody tell me how to archive this ?
我想在button click
和上触发两个动作button long click
。我UIbutton
在我的界面构建器中添加了一个。我怎样才能触发两个动作,IBAction
有人可以告诉我如何存档吗?
this is the code i have used for a button click
这是我用于单击按钮的代码
@IBAction func buttonPressed (sender: UIButton) {
....
}
@IBAction func buttonPressed (sender: UIButton) {
....
}
can i use this method or do i have to use another method for long click ?
我可以使用这种方法还是必须使用另一种方法进行长按?
回答by Dharmesh Kheni
If you want to perform any action with single tap you and long press the you can add gestures into button this way:
如果您想通过单击执行任何操作并长按 ,您可以通过以下方式将手势添加到按钮中:
@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")
}
This way you can add multiple method for single button and you just need Outlet for that button for that..
通过这种方式,您可以为单个按钮添加多个方法,而您只需要该按钮的 Outlet 即可。
回答by Murad Al Wajed
@IBOutlet weak var countButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
addLongPressGesture()
}
@IBAction func countAction(_ sender: UIButton) {
print("Single Tap")
}
@objc func longPress(gesture: UILongPressGestureRecognizer) {
if gesture.state == UIGestureRecognizerState.began {
print("Long Press")
}
}
func addLongPressGesture(){
let longPress = UILongPressGestureRecognizer(target: self, action: #selector(longPress(gesture:)))
longPress.minimumPressDuration = 1.5
self.countButton.addGestureRecognizer(longPress)
}
回答by Septronic
Why not create a custom UIButton class, create a protocol and let the button send back the info to delegte. Something like this:
为什么不创建一个自定义 UIButton 类,创建一个协议并让按钮将信息发送回委托。像这样的东西:
//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
}