This commit is contained in:
fmodf 2024-07-05 11:58:31 +02:00
parent ac850bfe4a
commit ce3bc42fbb

View file

@ -4,32 +4,39 @@ import SwiftUI
struct AttachmentLocationPickerView: View {
@StateObject var locationManager = LocationManager()
@State private var region = MKCoordinateRegion()
@State private var userInteraction = false
var body: some View {
ZStack {
// MapView
MapView(
region: $region,
userInteraction: $userInteraction
)
MapView(region: $region)
.onAppear {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
self.region = locationManager.region
}
}
.overlay {
Image(systemName: "mappin")
.foregroundColor(.Material.Elements.active)
.font(.system(size: 30))
.shadow(color: .white, radius: 2)
}
// Track button
let img = userInteraction ? "location.circle" : "location.circle.fill"
VStack {
Spacer()
HStack {
Spacer()
Image(systemName: img)
Image(systemName: "location.circle")
.resizable()
.frame(width: 30, height: 30)
.frame(width: 40, height: 40)
.foregroundColor(.Material.Elements.active)
.background(Color.Material.Shape.white)
.clipShape(Circle())
.padding(16)
.shadow(color: .white, radius: 2)
.padding(.trailing)
.padding(.bottom, 50)
.tappablePadding(.symmetric(10)) {
userInteraction = false
self.region = locationManager.region
}
}
}
@ -37,17 +44,11 @@ struct AttachmentLocationPickerView: View {
.onAppear {
locationManager.start()
}
.onChange(of: locationManager.region) { region in
if !userInteraction {
self.region = region
}
}
}
}
struct MapView: UIViewRepresentable {
@Binding var region: MKCoordinateRegion
@Binding var userInteraction: Bool
func makeUIView(context: Context) -> MKMapView {
let mapView = MKMapView()
@ -59,7 +60,7 @@ struct MapView: UIViewRepresentable {
}
func updateUIView(_ uiView: MKMapView, context _: Context) {
if uiView.region != region, !userInteraction {
if uiView.region != region {
uiView.setRegion(region, animated: true)
}
}