我想在 swiftUI Map 的地圖上添加 2 個按鈕來放大和縮小它。我已將螢屏底部的兩個按鈕放置在 VStack 內的 Stack 中。我需要代碼讓它們作業。
這就是我所擁有的:
import Foundation
import SwiftUI
import MapKit
struct QuakeDetail: View {
var quake: Quake
@State private var region : MKCoordinateRegion
init(quake : Quake) {
self.quake = quake
_region = State(wrappedValue: MKCoordinateRegion(center: quake.coordinate,
span: MKCoordinateSpan(latitudeDelta: 3, longitudeDelta: 3)))
}
var body: some View {
ZStack {
VStack {
Map(coordinateRegion: $region, annotationItems: [quake]) { item in
MapMarker(coordinate: item.coordinate, tint: .red)
} .ignoresSafeArea()
HStack {
Button {
print ("pressed ")
} label: {
HStack {
Text("map")
Image(systemName: "plus")
}
}.padding(5).border(Color.blue, width: 1)
Spacer()
QuakeMagnitude(quake: quake)
Spacer()
Button {
print ("pressed -")
} label: {
HStack {
Text("map")
Image(systemName: "minus")
}
}.padding(5).border(Color.blue, width: 1)
}.padding()
Text(quake.place)
.font(.headline)
.bold()
Text("\(quake.time.formatted())")
.foregroundStyle(Color.secondary)
Text("\(quake.latitude) \(quake.longitude)")
}
}
}
}
uj5u.com熱心網友回復:
您必須使用span引數 ofMKCoordinateRegion進行操作(值越小,縮放區域越大),例如(演示縮放 10%):
Button("Zoom In") {
region.span.latitudeDelta *= 0.9
region.span.longitudeDelta *= 0.9
}
使用 Xcode 13.2 / iOS 15.2 測驗
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/451075.html
上一篇:在字典中制作字典以將資料按一列中的相同值分隔,然后從第二列中分隔
下一篇:Python-錯誤,迭代字典串列
