|
| 1 | +// |
| 2 | +// UIViewRepresentableControlView.swift |
| 3 | +// |
| 4 | +// |
| 5 | +// Created by 老房东 on 2022-10-01. |
| 6 | +// |
| 7 | + |
| 8 | +import MapKit |
| 9 | +import SwiftUI |
| 10 | + |
| 11 | +struct UIViewRepresentableControlView: View { |
| 12 | + var body: some View { |
| 13 | + ScrollView { |
| 14 | + VStack { |
| 15 | + HeadlineView( |
| 16 | + title: "UIViewRepresentable", |
| 17 | + url: "https://developer.apple.com/documentation/swiftui/uiviewrepresentable", |
| 18 | + description: String(localized: "A wrapper for a UIKit view that you use to integrate that view into your SwiftUI view hierarchy.") |
| 19 | + ) |
| 20 | + CreateMapView() |
| 21 | + Divider() |
| 22 | + CreateMapViewWithAnnotation() |
| 23 | + } |
| 24 | + .padding() |
| 25 | + } |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +private struct CreateMapView: View { |
| 30 | + var code = """ |
| 31 | + import MapKit |
| 32 | + import SwiftUI |
| 33 | +
|
| 34 | + struct MapView: UIViewRepresentable { |
| 35 | + typealias UIViewType = MKMapView |
| 36 | + var region: MKCoordinateRegion |
| 37 | +
|
| 38 | + func makeUIView(context: Context) -> MKMapView { |
| 39 | + let mapView = MKMapView(frame: .zero) |
| 40 | + mapView.setRegion(region, animated: true) |
| 41 | + return mapView |
| 42 | + } |
| 43 | +
|
| 44 | + func updateUIView( |
| 45 | + _ uiView: MKMapView, |
| 46 | + context: Context) {} |
| 47 | + } |
| 48 | +
|
| 49 | + struct CreateMapView: View { |
| 50 | + @State private var region = MKCoordinateRegion( |
| 51 | + center: CLLocationCoordinate2D(latitude: 37.334803, longitude: -122.008965), |
| 52 | + span: MKCoordinateSpan(latitudeDelta: 1, longitudeDelta: 1) |
| 53 | + ) |
| 54 | +
|
| 55 | + var body: some View { |
| 56 | + VStack { |
| 57 | + Text("Create MapView") |
| 58 | + .font(.title2) |
| 59 | + MapView(region: region) |
| 60 | + .frame(width: 300, height: 200) |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + """ |
| 65 | + @State private var region = MKCoordinateRegion( |
| 66 | + // Apple Park |
| 67 | + center: CLLocationCoordinate2D(latitude: 37.334803, longitude: -122.008965), |
| 68 | + span: MKCoordinateSpan(latitudeDelta: 1, longitudeDelta: 1) |
| 69 | + ) |
| 70 | + |
| 71 | + var body: some View { |
| 72 | + VStack { |
| 73 | + Text("Create a MapView from MKMapView") |
| 74 | + .font(.title2) |
| 75 | + CodePreviewView(code: code) |
| 76 | + MapView(region: $region) |
| 77 | + .frame(width: 300, height: 200) |
| 78 | + } |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +private struct CreateMapViewWithAnnotation: View { |
| 83 | + var code = """ |
| 84 | + struct CreateMapViewWithAnnotation: View { |
| 85 | + @State private var region = MKCoordinateRegion( |
| 86 | + center: CLLocationCoordinate2D(latitude: 37.334803, longitude: -122.008965), |
| 87 | + span: MKCoordinateSpan(latitudeDelta: 1, longitudeDelta: 1) |
| 88 | + ) |
| 89 | +
|
| 90 | + @State private var annotations: [MKAnnotation] = [ |
| 91 | + MyAnnotation(title: "Apple", subtitle: "I want to go", coordinate: CLLocationCoordinate2D(latitude: 37.334803, longitude: -122.008965)) |
| 92 | + ] |
| 93 | +
|
| 94 | + var body: some View { |
| 95 | + Text("Create MapView with corrdinator") |
| 96 | + .font(.title2) |
| 97 | + MapView(region: $region, annotations: annotations) |
| 98 | + .frame(width: 300, height: 200) |
| 99 | + Text("latitude:\\(region.center.latitude, specifier: "%.2f") longitude\\(region.center.longitude, specifier: "%.2f")") |
| 100 | + HStack { |
| 101 | + Button("Add Annotations") { |
| 102 | + let number = annotations.count |
| 103 | + annotations.append( |
| 104 | + MyAnnotation( |
| 105 | + title: "Apple\\(number)", |
| 106 | + subtitle: "I want to go\\(number)", |
| 107 | + coordinate: CLLocationCoordinate2D(latitude: 37.334803, longitude: -122.008965 - Double(number) * 0.3) |
| 108 | + ) |
| 109 | + ) |
| 110 | + } |
| 111 | + Button("Remove Annotations") { |
| 112 | + if annotations.isEmpty == false { |
| 113 | + annotations.removeLast() |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | +
|
| 120 | + class MyAnnotation: NSObject, MKAnnotation { |
| 121 | + let coordinate: CLLocationCoordinate2D |
| 122 | + let title: String? |
| 123 | + let subtitle: String? |
| 124 | +
|
| 125 | + init(title: String?, |
| 126 | + subtitle: String?, |
| 127 | + coordinate: CLLocationCoordinate2D) |
| 128 | + { |
| 129 | + self.title = title |
| 130 | + self.subtitle = subtitle |
| 131 | + self.coordinate = coordinate |
| 132 | + } |
| 133 | + } |
| 134 | +
|
| 135 | + struct MapView: UIViewRepresentable { |
| 136 | + typealias UIViewType = MKMapView |
| 137 | +
|
| 138 | + @Binding var region: MKCoordinateRegion |
| 139 | + var lineCoordinates: [CLLocation] = [] |
| 140 | + var annotations: [MKAnnotation] = [] |
| 141 | +
|
| 142 | + class Coordinator: NSObject, MKMapViewDelegate { |
| 143 | + var control: MapView |
| 144 | +
|
| 145 | + init(_ control: MapView) { |
| 146 | + self.control = control |
| 147 | + } |
| 148 | + |
| 149 | + func mapViewDidChangeVisibleRegion(_ mapView: MKMapView) { |
| 150 | + control.region = mapView.region |
| 151 | + } |
| 152 | + } |
| 153 | +
|
| 154 | + func makeUIView(context: Context) -> MKMapView { |
| 155 | + let mapView = MKMapView(frame: .zero) |
| 156 | + mapView.delegate = context.coordinator |
| 157 | + mapView.setRegion(region, animated: true) |
| 158 | + return mapView |
| 159 | + } |
| 160 | +
|
| 161 | + func updateUIView(_ mapView: MKMapView, context: Context) { |
| 162 | + mapView.removeAnnotations(mapView.annotations) |
| 163 | + mapView.addAnnotations(annotations) |
| 164 | + } |
| 165 | +
|
| 166 | + func makeCoordinator() -> Coordinator { |
| 167 | + Coordinator(self) |
| 168 | + } |
| 169 | + } |
| 170 | + """ |
| 171 | + |
| 172 | + @State private var region = MKCoordinateRegion( |
| 173 | + // Apple Park |
| 174 | + center: CLLocationCoordinate2D(latitude: 37.334803, longitude: -122.008965), |
| 175 | + span: MKCoordinateSpan(latitudeDelta: 1, longitudeDelta: 1) |
| 176 | + ) |
| 177 | + |
| 178 | + @State private var annotations: [MKAnnotation] = [ |
| 179 | + MyAnnotation(title: "Apple", subtitle: "I want to go", coordinate: CLLocationCoordinate2D(latitude: 37.334803, longitude: -122.008965)) |
| 180 | + ] |
| 181 | + |
| 182 | + var body: some View { |
| 183 | + Text("Create a MapView using corrdinator") |
| 184 | + .font(.title2) |
| 185 | + CodePreviewView(code: code) |
| 186 | + MapView(region: $region, annotations: annotations) |
| 187 | + .frame(width: 300, height: 200) |
| 188 | + Text("latitude:\(region.center.latitude, specifier: "%.2f") longitude\(region.center.longitude, specifier: "%.2f")") |
| 189 | + HStack { |
| 190 | + Button("Add Annotations") { |
| 191 | + let number = annotations.count |
| 192 | + annotations.append( |
| 193 | + MyAnnotation( |
| 194 | + title: "Apple\(number)", |
| 195 | + subtitle: "I want to go\(number)", |
| 196 | + coordinate: CLLocationCoordinate2D(latitude: 37.334803, longitude: -122.008965 - Double(number) * 0.3) |
| 197 | + ) |
| 198 | + ) |
| 199 | + } |
| 200 | + Button("Remove Annotations") { |
| 201 | + if annotations.isEmpty == false { |
| 202 | + annotations.removeLast() |
| 203 | + } |
| 204 | + } |
| 205 | + } |
| 206 | + } |
| 207 | +} |
| 208 | + |
| 209 | +private class MyAnnotation: NSObject, MKAnnotation { |
| 210 | + let coordinate: CLLocationCoordinate2D |
| 211 | + let title: String? |
| 212 | + let subtitle: String? |
| 213 | + |
| 214 | + init(title: String?, |
| 215 | + subtitle: String?, |
| 216 | + coordinate: CLLocationCoordinate2D) |
| 217 | + { |
| 218 | + self.title = title |
| 219 | + self.subtitle = subtitle |
| 220 | + self.coordinate = coordinate |
| 221 | + } |
| 222 | +} |
| 223 | + |
| 224 | +private struct MapView: UIViewRepresentable { |
| 225 | + typealias UIViewType = MKMapView |
| 226 | + |
| 227 | + @Binding var region: MKCoordinateRegion |
| 228 | + var lineCoordinates: [CLLocation] = [] |
| 229 | + var annotations: [MKAnnotation] = [] |
| 230 | + |
| 231 | + class Coordinator: NSObject, MKMapViewDelegate { |
| 232 | + var control: MapView |
| 233 | + |
| 234 | + init(_ control: MapView) { |
| 235 | + self.control = control |
| 236 | + } |
| 237 | + |
| 238 | + func mapViewDidChangeVisibleRegion(_ mapView: MKMapView) { |
| 239 | + control.region = mapView.region |
| 240 | + } |
| 241 | + } |
| 242 | + |
| 243 | + func makeUIView(context: Context) -> MKMapView { |
| 244 | + let mapView = MKMapView(frame: .zero) |
| 245 | + mapView.delegate = context.coordinator |
| 246 | + mapView.setRegion(region, animated: true) |
| 247 | + return mapView |
| 248 | + } |
| 249 | + |
| 250 | + func updateUIView(_ mapView: MKMapView, context: Context) { |
| 251 | + mapView.removeAnnotations(mapView.annotations) |
| 252 | + mapView.addAnnotations(annotations) |
| 253 | + } |
| 254 | + |
| 255 | + func makeCoordinator() -> Coordinator { |
| 256 | + Coordinator(self) |
| 257 | + } |
| 258 | +} |
| 259 | + |
| 260 | +struct UIViewRepresentableControlView_Previews: PreviewProvider { |
| 261 | + static var previews: some View { |
| 262 | + UIViewRepresentableControlView() |
| 263 | + } |
| 264 | +} |
0 commit comments