gpt4 book ai didi

ios - SwiftUI Map iOS 14 处理拖动和放大手势

转载 作者:行者123 更新时间:2023-12-04 17:15:20 24 4
gpt4 key购买 nike

如果用户拖动或缩放 map ,我会在 map 顶部放置一个小 View ,我使用下面的代码来执行此操作,但未触发缩放事件。请帮助我我所缺少的

    let drag = DragGesture()
.onChanged({ _ in
self.onDragEnded()
})
let pinch = MagnificationGesture()
.onChanged({ _ in
self.onDragEnded()
})
@State private var show = false

let combinedGesture = pinch.simultaneously(with: drag)

var body: some View {

ZStack {
Map(coordinateRegion: $region, showsUserLocation: true, annotationItems: results) { dest in

}.gesture(combinedGesture)
if show {
Color.green
.frame(width: 100, height: 100, alignment: .center)
}
}
}
private func onDragEnded() {
show = true

}

最佳答案

当用户拖动和/或缩放 map 时,区域 会发生变化。您只需观察 MKCoordinateRegion 或其两个属性(span 或 center)之一:

.onChange(of: region.span) {_ in
print("regionDidChange")
}

您使用 onChange 观察到的属性必须是 Equatable :

extension MKCoordinateSpan: Equatable {
public static func == (lhs: MKCoordinateSpan, rhs: MKCoordinateSpan) -> Bool {
lhs.latitudeDelta == rhs.latitudeDelta && lhs.longitudeDelta == rhs.longitudeDelta
}
}

编辑:

但是如果你想在position改变的时候和zoom改变的时候执行不同的 Action ,那就比较复杂了。因为 1- 缩放级别取决于位置和跨度。2-当用户放大或缩小时,中心发生变化(非常轻微)3- 当中心改变时跨度改变(一点点)

你可以这样开始:

struct MapView: View {
@State private var region = MKCoordinateRegion(center: .init(latitude: 42, longitude: 1.2), span: .init(latitudeDelta: 10, longitudeDelta: 10))
@State private var drag: Int = 0
@State private var pinch: Int = 0
@State private var oldZoomLevel: Double?

var body: some View {
ZStack(alignment: Alignment(horizontal: .center, vertical: .top)) {

GeometryReader { proxy in
Map(coordinateRegion: $region)
.onChange(of: region) { newRegion in
let zlevel = getZoomLevel(mapWidth: Double(proxy.size.width))
if zlevel != oldZoomLevel {
pinch += 1
} else {
drag += 1
}
oldZoomLevel = zlevel
}
}

HStack {
Text(drag.description)
.padding()
.background(Color.pink)
Spacer()
Text(pinch.description)
.padding()
.background(Color.yellow)
}
}
}

func getZoomLevel(mapWidth: Double) -> Double {
let MERCATOR_RADIUS = 85445659.44705395
let level = 20.00 - log2(region.span.longitudeDelta * MERCATOR_RADIUS * Double.pi / (180.0 * mapWidth))
return round(level * 100000)/100000
}
}

和扩展:

extension CLLocationCoordinate2D: Equatable {
public static func == (lhs: CLLocationCoordinate2D, rhs: CLLocationCoordinate2D) -> Bool {
lhs.latitude == rhs.latitude && lhs.longitude == rhs.longitude
}
}

extension MKCoordinateSpan: Equatable {
public static func == (lhs: MKCoordinateSpan, rhs: MKCoordinateSpan) -> Bool {
lhs.latitudeDelta == rhs.latitudeDelta && lhs.longitudeDelta == rhs.longitudeDelta
}
}

extension MKCoordinateRegion: Equatable {
public static func == (lhs: MKCoordinateRegion, rhs: MKCoordinateRegion) -> Bool {
lhs.center == rhs.center && lhs.span == rhs.span
}
}

关于ios - SwiftUI Map iOS 14 处理拖动和放大手势,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68801219/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com