UIButton with single press and long press events swift
40,651
Solution 1
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..
Solution 2
@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)
}
Related videos on Youtube
Comments
-
Kamal Upasena almost 2 years
I want to trigger two action on
button click
andbutton long click
. I have add aUIbutton
in my interface builder. How can i trigger two action usingIBAction
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 ?
-
Yatheesha almost 9 yearsCheck this one stackoverflow.com/questions/6660282/…
-
-
Kamal Upasena almost 9 yearsi have multiple buttons in my ui since they all do the same this i have map them in to this method (
@IBAction func buttonPressed (sender: UIButton
)and can i add method programatically ? -
Dharmesh Kheni almost 9 yearsThen you need to create outlet for all button and add this gestures into it.and this method will work for all buttons.
-
Kamal Upasena almost 9 yearsoh i have 30 buttons in my ui its a keyboard :( is there any way to archive them without using outlet ?
-
Dharmesh Kheni almost 9 yearsI think no. you have to create outlet and add gestures for all If you want to perform action with long press.:)
-
Kamal Upasena almost 9 yearscan we map a IBAction programatically ? so then i only have to create outlet for a one button only
-
Dharmesh Kheni almost 9 yearsyes you can use this
button.addTarget(self, action: "buttonTapAction:", forControlEvents: UIControlEvents.TouchUpInside)
-
Kamal Upasena almost 9 yearswhen i try to add the gesture i am getting error like this
fatal error: unexpectedly found nil while unwrapping an Optional value
any idea ? -
Dharmesh Kheni almost 9 yearsLet us continue this discussion in chat.
-
Samarth Kejriwal about 5 years@Dharmesh I am getting this issue
unrecognized selector sent to instance 0x2802ec000
, I am using the following code snippet for the tapFunction,@objc func longPress(_ btn : UIButton) {}