有人可以使用 MapKit+LongPress 吗?
我正在尝试让 SwiftUI + MapKit + LongPress 手势正常工作。当我在 ContentView
中添加地图时,效果很好。然后,我将 .onLongPressGesture
修改器添加到地图中,平移/缩放停止工作。长按虽然有效。
我正在处理的代码:
Map(coordinateRegion: $region, interactionModes: .all, showsUserLocation: true)
.onLongPressGesture {
// How do I get the location (Lat/Long) I am pressed on?
print("onLongPressGesture")
}
另外,有没有办法从长按手势获取纬度/经度?
希望仅使用 SwiftUI 使其工作,而不将 UIKit 包装在 UIViewRepresentable 中。
I am trying to get SwiftUI + MapKit + LongPress gesture working. When I add the map within the ContentView
, it works great. I then add the .onLongPressGesture
modifier to the map, and the panning/zooming stops working. Long press works though.
The code I am working on:
Map(coordinateRegion: $region, interactionModes: .all, showsUserLocation: true)
.onLongPressGesture {
// How do I get the location (Lat/Long) I am pressed on?
print("onLongPressGesture")
}
Also, is there a way to get the lat/long from the long press gesture?
Looking to make it work using SwiftUI
only, without wrapping UIKit
within a UIViewRepresentable
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
不要问为什么,但这似乎有效:
Don't ask why but this seems to work:
以下是我如何获取 LongPressGesture 的位置,同时保留地图初始值设定项的
selection
参数的功能(即现有地图对象上不允许长按):或者,这里是对
Map
类型以减少样板:它的使用方式如下:
SpacialTapGesture
从 iOS 16+ 开始可用。Here's how I was able to get the location of a LongPressGesture, while also preserving the functionality of the Map initializer's
selection
parameter (i.e. long pressing not allowed on existing map objects):Alternatively, here's an extension on the
Map
type to reduce boilerplate:It would be used like so:
SpacialTapGesture
is available from iOS 16+.iOS 18
从 iOS 18 开始,我们有 UIGestureRecognizerRepresentable 我们可以使用任何
UIGestureRecognizer
像SwiftUI
中的 UILongPressGestureRecognizer。示例
地图
手势识别器
最后注释
请记住,截至 2024 年 6 月,这仍处于测试阶段,可能会很快发生变化。
有关此内容的更多详细信息:WWDC 24 SwiftUI 中的新增功能
iOS 18
Starting on iOS 18 we have UIGestureRecognizerRepresentable and we can use any
UIGestureRecognizer
like UILongPressGestureRecognizer inSwiftUI
.Example
The map
The gesture recognizer
Final notes
Remember as of June 2024 this is still in Beta and may change quickly.
For more details on this: WWDC 24 What's new in SwiftUI
是的。
我将
MKMapView
包裹在UIViewRepresentable
中,并在委托方法中添加了长按手势识别器,如下所示:然后
使用此答案中的代码: https://stackoverflow.com/a/71698741/1320010
Yep.
I wrapped
MKMapView
inUIViewRepresentable
and added long press gesture recognizer in the delegate method like this:Then
Using the code from this answer: https://stackoverflow.com/a/71698741/1320010
要回答问题的最后一部分 - Paul Hudson 展示了如何在 此页面 [编辑 - 长按似乎不起作用]
To answer the last part of your question - Paul Hudson shows how to get the tap location in lat/long on this page [EDIT - doesn't seem to work for long press]