π΄ Let's Build UBER with SwiftUI | iOS 16 & Xcode 14
UberClone: MapView 2
ꡬν λͺ©ν
- 맡뷰 μν μ
λ°μ΄νΈ
ꡬν νμ€ν¬
- μ μ μ 보 μ ν κΈ°μ€ μ΄λν
- κ²½λ‘ μ κ±° λ° μ΄κΈ°ν ν¨μ ꡬν
ν΅μ¬ μ½λ
import Foundation
enum MapViewState {
case noInput
case locationSelected
case searchingForLocation
}
- νΉμ μ§μ μ ν μΌμ΄μ€λ₯Ό μ‘°μνκΈ° μν μ΄λ
private func actionForState(_ state: MapViewState) {
switch state {
case .noInput:
print("no input")
case .locationSelected, .searchingForLocation:
mapState = .noInput
}
}
- λ²νΌ ν κΈλ§μ ν΅ν 맡 μν μ‘°μ
.onTapGesture {
withAnimation(.spring()) {
viewModel.selectLocation(with: result)
mapState = .locationSelected
}
}
- νΉμ 리μ€νΈ μ
ν΄λ¦ μ μ§μμ μ νν μν©μΌλ‘ μ ν
func updateUIView(_ uiView: UIViewType, context: Context) {
switch mapState {
case .noInput:
context.coordinator.clearMapViewAndRecenterLocation()
case .searchingForLocation:
break
case .locationSelected:
if let selectedLocation = viewModel.selectedLocation {
context.coordinator.addAndSelectAnnotation(with: selectedLocation)
context.coordinator.configurePolyline(with: selectedLocation)
}
}
}
- νμ¬ μν©μ΄ μΈνμ΄ μλ€λ©΄ μ΄κΈ°ν, μ§μμ μ ννλ€λ©΄ ν΄λΉ μ§μμΌλ‘ μ΄μ΄μ§λ κ²½λ‘λ₯Ό μλμΌλ‘ νμ
func clearMapViewAndRecenterLocation() { parent.mapView.removeAnnotations(parent.mapView.annotations)
parent.mapView.removeOverlays(parent.mapView.overlays)
guard let currentCoordinate = userLocationCoordinate else { return }
setRegion(with: currentCoordinate)
}
- 맡뷰 λ΄ μ‘΄μ¬νλ μ΄λ
Έν
μ΄μ
κ³Ό κ²½λ‘ λͺ¨λ μμ
- κΈ°μ‘΄μ μ μ λ³μλ‘ μ μΈν μ΄κΈ° μ μ μμΉ μ 보λ₯Ό ν΅ν΄ ν¬μ»€μ€λ μ§μ μΈν
ꡬν νλ©΄