UIButton的单按和长按事件很快 [英] UIButton with single press and long press events swift
问题描述
我想在按钮上单击
和按钮长按
。我在界面构建器中添加了 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(发件人: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, action: "Tap") //Tap function will call when user tap on button
let longGesture = UILongPressGestureRecognizer(target: self, action: "Long") //Long function will call when user long press on button.
tapGesture.numberOfTapsRequired = 1
btn.addGestureRecognizer(tapGesture)
btn.addGestureRecognizer(longGesture)
}
func Tap() {
println("Tap happend")
}
func Long() {
println("Long press")
}
这样你就可以为单个按钮添加多个方法,你只需要为该按钮选择Outlet。 。
This way you can add multiple method for single button and you just need Outlet for that button for that..
这篇关于UIButton的单按和长按事件很快的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!