Swift: Long Press Gesture Recognizer - Detect taps and Long Press
Define two IBActions
and set one Gesture Recognizer
to each of them. This way you can perform two different actions for each gesture.
You can set each Gesture Recognizer
to different IBActions in the interface builder.
@IBAction func tapped(sender: UITapGestureRecognizer)
{
println("tapped")
//Your animation code.
}
@IBAction func longPressed(sender: UILongPressGestureRecognizer)
{
println("longpressed")
//Different code
}
Through code without interface builder
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: "tapped:")
self.view.addGestureRecognizer(tapGestureRecognizer)
let longPressRecognizer = UILongPressGestureRecognizer(target: self, action: "longPressed:")
self.view.addGestureRecognizer(longPressRecognizer)
func tapped(sender: UITapGestureRecognizer)
{
println("tapped")
}
func longPressed(sender: UILongPressGestureRecognizer)
{
println("longpressed")
}
Swift 5
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(tapped))
self.view.addGestureRecognizer(tapGestureRecognizer)
let longPressRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(longPressed))
self.view.addGestureRecognizer(longPressRecognizer)
@objc func tapped(sender: UITapGestureRecognizer){
print("tapped")
}
@objc func longPressed(sender: UILongPressGestureRecognizer) {
print("longpressed")
}
Customize long press gesture recognizer
The answer from @nathan is essentially fine but a detail is missing you need to implement the UIGestureRecognizerDelegate
to allow both gestures works simultaneously, so this is my code
class ViewController: UIViewController, UIGestureRecognizerDelegate{
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
//this for .5 time
let firstGesture = UILongPressGestureRecognizer(target: self, action: #selector(firstMethod))
//this for 1.5
let secondGesture = UILongPressGestureRecognizer(target: self, action: #selector(secondMethod))
secondGesture.minimumPressDuration = 1.5
firstGesture.delegate = self
secondGesture.delegate = self
self.view.addGestureRecognizer(firstGesture)
self.view.addGestureRecognizer(secondGesture)
}
func firstMethod() {
debugPrint("short")
}
func secondMethod() {
debugPrint("long")
}
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool{
return true
}
}
Hope this help
How to get the button text if a long press gesture on the button is triggered in swift?
Do
@objc func clearBottom(sender: UILongPressGestureRecognizer) {
if sender.state == .ended {
// how do I get the button text here? (i.e. "Item Name AAA")
let button = sender.view as! UIButton
print(button.titleLabel?.text)
}
}
Resolve UITapGestureRecognizer and UILongPressGestureRecognizer simultaneously and fire UIGestureRecognizer.State.began on finger touch down
I wonder if your implementation of shouldRequireFailureOf
is causing an issue?
This works just fine for me (note: I used .minimumPressDuration = 0.25
because it's a little difficult to tap in under 0.1 seconds):
class GestureTestViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
setupTapGestures()
}
private func setupTapGestures() -> Void {
let singleTapGesture = UITapGestureRecognizer(target: self, action: #selector(handleTapGesture(_:)))
view.addGestureRecognizer(singleTapGesture)
let longPressGesture = UILongPressGestureRecognizer(target: self, action: #selector(handleLongPressGesture(_:)))
longPressGesture.minimumPressDuration = 0.25
view.addGestureRecognizer(longPressGesture)
}
@objc func handleLongPressGesture(_ gesture: UILongPressGestureRecognizer) -> Void {
if gesture.state == .began {
print("Did Long Press (began)")
}
if gesture.state == .ended {
print("Did Long Press (ended)")
}
}
@objc func handleTapGesture(_ gesture: UITapGestureRecognizer) -> Void {
print("Did Single Tap")
}
}
When I tap, I get "Did Single Tap" in the debug console.
When I tap and hold, I quickly get "Did Long Press (began)", and on finger-lift I get "Did Long Press (ended)"
Using tap gesture and long press at the same time in Table View
UILongPressGestureRecognizer.state
should be sender.state
and UILongPressGesutreRecognizer.location
should be sender.location
. Also, the signature for indexPathForRowAtPoint()
has been updated to indexPathForRow(at:)
.
Corrrected code:
@objc func handleLongPress(sender: UILongPressGestureRecognizer) {
if sender.state == .began {
let touchPoint = sender.location(in: self.myTableView)
if let indexPath = self.myTableView.indexPathForRow(at:touchPoint) {
print(indexPath.row)
}
}
}
UILongPressGestureRecognizer
is a class name, you need to be calling the class instance.
Create long press gesture recognizer with annotation pin
First of all in Swift 3 signature of CLLocationManagerDelegate
method's locationManager(_:didUpdateLocations:)
is changed, so you need to change that method as follow.
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
//your code and don't forgot to remove private
}
You can use longGesture
with mapView
like this, first addGestureRecognizer
in your mapView
in the viewDidLoad
.
let longPressGesture = UILongPressGestureRecognizer(target: self, action: #selector(addAnnotationOnLongPress(gesture:)))
longPressGesture.minimumPressDuration = 1.0
self.mapView.addGestureRecognizer(longPressGesture)
Now add action for that UILongPressGestureRecognizer
.
@objc func addAnnotationOnLongPress(gesture: UILongPressGestureRecognizer) {
if gesture.state == .ended {
let point = gesture.location(in: self.mapView)
let coordinate = self.mapView.convert(point, toCoordinateFrom: self.mapView)
print(coordinate)
//Now use this coordinate to add annotation on map.
var annotation = MKPointAnnotation()
annotation.coordinate = coordinate
//Set title and subtitle if you want
annotation.title = "Title"
annotation.subtitle = "subtitle"
self.mapView.addAnnotation(annotation)
}
}
How to detect longpress in BarButtonitem
so I found that UIBarButton has not property like longpress so all I do is take a UIButton give it longpress gesture and add that UIButton in navigation bar as UIBarButtonItem.
I hope it will helpful for someone else who is facing same problem.
let btn = UIButton(frame: CGRect(x: 0, y: 0, width: 40, height: 40))
btn.backgroundColor = .green
let gesture = UILongPressGestureRecognizer(target: self, action: #selector(longpress))
btn.addGestureRecognizer(gesture)
let barbtn = UIBarButtonItem(customView: btn)
self.navigationItem.rightBarButtonItem = barbtn
thank you :)
How to run function when long press gesture detected and stopped?
I could solve my problem, hope it's useful for some of you:
@State private var isPressingDown: Bool = false
Image(systemName: "mic.circle")
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: 70)
.foregroundColor(Color.blue)
.onLongPressGesture(minimumDuration: 0.3){
self.isPressingDown = true
print("started")
}
.simultaneousGesture(
DragGesture(minimumDistance: 0)
.onEnded{ _ in
if self.isPressingDown{
self.isPressingDown = false
print("ended")
}
}
)
Related Topics
Datefromstring Returns Nil for Some Values
Atos and Dwarfdump Won't Symbolicate My Address
Disable Bitcode for Project and Cocoapods Dependencies with Xcode 7
How to Change the Color of an Uiimage
Run iPhone as an Ibeacon in the Background
How to Invoke iPhone Maps for Directions with Current Location as Start Address
Is There Any Way of Asking an iOS View Which of Its Children Has First Responder Status
How to Hide 'Back' Button on Navigation Bar on Iphone
How to Present View Controller from Right to Left in iOS Using Swift
How to Limit Uitableview Row Reordering to a Section
iPhone Uitableview. How Do Turn on the Single Letter Alphabetical List Like the Music App
Wkwebview and Nsurlprotocol Not Working
Capture Uiview and Save as Image
Coremotion Updates in Background State