【问题标题】:Apple Watch SwiftUI Map events? How do I read the current region span on digital crown rotation?Apple Watch SwiftUI 地图事件?如何读取数字表冠旋转的当前区域跨度?
【发布时间】:2021-09-17 00:28:11
【问题描述】:

我的 Apple Watch SwiftUI 应用程序上有一个启用缩放功能的地图,我想在用户旋转数字表冠进行放大或缩小时保存当前的缩放级别或区域跨度,以防止缩放发生变化新的用户职位到来。这甚至可能吗?如何使用 onChange 读取 Map.region.span 属性?

【问题讨论】:

    标签: swiftui mapkit watchkit apple-watch


    【解决方案1】:

    这是我在 watchOS 应用中使用的视图。如您所见,地图的初始化程序允许您使用所有这些不同的参数。因为这个视图是从提供信息的另一个视图中调用的,所以我使用Bindings 作为参数,但是如果视图是自包含的,我可以很容易地使用State 等。当然,任何改变在地图上,就是体现在这些变量上。我传入的变量之一是区域。由于它在地图上以图形方式更改,因此在变量中也发生了更改。

    struct WatchMapView: View {
        
        @EnvironmentObject var location: GeoLocation
        
        @ObservedObject var updater = Updater.shared
    
        @Binding var region: MKCoordinateRegion
        let userTrackingMode: MapUserTrackingMode = .none
        @Binding var currentPlace: Place
        @Binding var places: [Place]
    
        var body: some View {
            ZStack {
                Map(
                    coordinateRegion: $region,
                    interactionModes: MapInteractionModes.all,
                    showsUserLocation: false,
                    userTrackingMode: $userTrackingMode,
                    annotationItems: places) { place in
                    MapMarker(coordinate: place.coordinate, tint: .red)
                }
            }
        }
    }
    

    【讨论】:

    • 事实上,我从视图外部的 environmentObject 更新了区域,但在您回答之前,我没有意识到绑定变量将以两种方式工作。现在看来很明显。谢谢!
    猜你喜欢
    • 2021-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多