问题描述
我想在 button click
和 button long click
上触发两个动作.我在界面生成器中添加了一个 UIbutton
.我如何使用 IBAction
触发两个动作有人可以告诉我如何存档吗?
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 ?
这是我用来点击按钮的代码
this is the code i have used for a button click
@IBAction func buttonPressed (sender: UIButton) {……}
我可以使用这种方法还是必须使用其他方法进行长按?
can i use this method or do i have to use another method for long click ?
推荐答案
如果您想通过单击执行任何操作并长按您可以通过这种方式将手势添加到按钮中:
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")
}
这样你可以为单个按钮添加多个方法,你只需要那个按钮的 Outlet ..
This way you can add multiple method for single button and you just need Outlet for that button for that..
这篇关于具有单按和长按事件的 UIButton 快速的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!