[SwiftUI] UberClone: MapView 2

Junyoung ParkΒ·2022λ…„ 11μ›” 19일
0

SwiftUI

λͺ©λ‘ 보기
116/136
post-thumbnail

πŸ”΄ 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)
        }
  • 맡뷰 λ‚΄ μ‘΄μž¬ν•˜λŠ” μ–΄λ…Έν…Œμ΄μ…˜κ³Ό 경둜 λͺ¨λ‘ μ‚­μ œ
  • 기쑴에 μ „μ—­ λ³€μˆ˜λ‘œ μ„ μ–Έν•œ 초기 μœ μ € μœ„μΉ˜ 정보λ₯Ό 톡해 포컀슀된 지역 μ„ΈνŒ…

κ΅¬ν˜„ ν™”λ©΄

profile
JUST DO IT

0개의 λŒ“κΈ€