GO FURTHER, FASTER: Try the Swift Career Accelerator today! >>

Long press gestures in the simulator

Forums > 100 Days of SwiftUI

I'm working on the BucketList project, and I'm to the point of implementing the long press gesture. But the gesture isn't being picked up in the simulator or in preview. I tried adding 'print("Received long press")' to .onLongPressGesture, and nothing gets printed in the console for long presses, but 'print' statements in .onTapGesture are shown.

This is where I disclose that I'm running Xcode 16 beta 3 on macOS 15 beta 3, so chances of this problem being related to that are not zero. :-)

   

I also tried this on a physical device, and long presses don't work there either.

   

I tried a simple image with .onLongPressGesture attached, and with the action set to just toggle the color of the star. That works fine.

So then I pulled out the MapReader block of code from the BucketList project, minus the ForEach loop, so it just has a single annotation, but that doesn't work. It displays the map and the annotation point, but a long press on the star doesn't do anything.

Not sure what I'm missing. Any ideas? (I tried to post the whole ContentView.swift file, but it gets blocked as spam for some reason.)

    var body: some View {
        MapReader { proxy in
            Map(initialPosition: startPosition) {
                Annotation("Here", coordinate: coordinate) {
                    Image(systemName: "star.fill")
                        .resizable()
                        .frame(width: 44, height: 44)
                        .foregroundColor(isPressed ? .blue : .red)
                        .onLongPressGesture {
                            isPressed.toggle()
                        }
                }
            }
        }
    }

1      

I had the same question.

For debugging on Simulator and Preview i add selectedPlace = locations.first after locations.append(newLocation)

Now after drop a new pin — EditView sheet is presented immidiately

1      

I'm having the same issue with long press gesture not working in canvas or simulator. I'm on Xcode 16.0 general release. There are also people complaining about this in the Apple developer forums, so this is a known bug. Hopefully it gets fixed soon!

1      

I solved the issue using the simultaneous Gesture

.simultaneousGesture(LongPressGesture(minimumDuration: 1).onEnded { _ in selectedPlace = location })

8      

You nailed it! Thanks!,

How did you find this solution?, trying to learn how to learn

   

My guess is the Map's gestures take priority here so by using .simultaneousGesture, you get to have SwiftUI recognize the LongPressGesture alongside other gestures

   

Thanks @cr34t1ve!

Your solution works great.

The simultaneousGesture doc says, "Use this method when you need to define and process a view specific gesture simultaneously with the same priority as the view’s existing gestures. "

FWIW, I noticed using print statements that MapReader was triggering but LongPress wasn't. That seems to match what this is for too. I would have guessed events would have bubbled up like in JS. Guess not.

   

Hacking with Swift is sponsored by try! Swift Tokyo.

SPONSORED Ready to dive into the world of Swift? try! Swift Tokyo is the premier iOS developer conference will be happened in April 9th-11th, where you can learn from industry experts, connect with fellow developers, and explore the latest in Swift and iOS development. Don’t miss out on this opportunity to level up your skills and be part of the Swift community!

Get your ticket here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.