Post

Replies

Boosts

Views

Activity

Reply to `onTapGesture` not triggered on `Map` views
For those having to deal with that bug and in need to get location for the tap, I've used the answer here https://stackoverflow.com/questions/56513942/how-to-detect-a-tap-gesture-location-in-swiftui with a slight modification : import SwiftUI struct ClickGesture: Gesture { let count: Int let coordinateSpace: CoordinateSpace typealias Value = SimultaneousGesture<TapGesture, DragGesture>.Value init(count: Int = 1, coordinateSpace: CoordinateSpace = .local) { precondition(count > 0, "Count must be greater than or equal to 1.") self.count = count self.coordinateSpace = coordinateSpace } var body: SimultaneousGesture<TapGesture, DragGesture> { SimultaneousGesture( TapGesture(count: count), DragGesture(minimumDistance: 0, coordinateSpace: coordinateSpace) ) } func onEnded(perform action: @escaping (CGPoint) -> Void) -> _EndedGesture<ClickGesture> { self.onEnded { (value: Value) -> Void in guard value.first != nil else { return } guard let location = value.second?.startLocation else { return } guard let endLocation = value.second?.location else { return } guard ((location.x-1)...(location.x+1)).contains(endLocation.x), ((location.y-1)...(location.y+1)).contains(endLocation.y) else { return } action(location) } } } extension View { func onClickGesture( count: Int, coordinateSpace: CoordinateSpace = .local, perform action: @escaping (CGPoint) -> Void ) -> some View { simultaneousGesture(ClickGesture(count: count, coordinateSpace: coordinateSpace) .onEnded(perform: action) ) } func onClickGesture( count: Int, perform action: @escaping (CGPoint) -> Void ) -> some View { onClickGesture(count: count, coordinateSpace: .local, perform: action) } func onClickGesture( perform action: @escaping (CGPoint) -> Void ) -> some View { onClickGesture(count: 1, coordinateSpace: .local, perform: action) } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Sep ’25