iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 21
0
Mobile Development

[初探] 用 SwiftUI - 打造一個自助洗衣應用系列 第 21

第二十一日、MapKit 篇 - 開始

當我們開始進入 MapKit 使用的時候
我們要先思考想兩件事情
其一
- 將地圖顯示在視圖中
其二
- SwiftUI 狀態變化時 地圖視圖的更新

那也就是實現出 makeUIView()updateUIView() 方法

struct MapView: UIViewRepresentable {
    func makeUIView(context: Context) -> MKMapView {
        let mapView = MKMapView()
        //**其中**
        return mapView
    }

    func updateUIView(_ view: MKMapView, context: Context) {
    }
}

makeUIView()

Creates the view object and configures its initial state.

創造視圖並初始化它的狀態
context參考量為包含前後狀態的信息。
你可以在其中設定當前座標位置並使其實現

let center = CLLocationCoordinate2D(latitude: 24.178693 , longitude: 120.646740)
let region = MKCoordinateRegion(center: center,
                               latitudinalMeters: CLLocationDistance(1000),
                               longitudinalMeters: CLLocationDistance(1000))
mapView.setRegion(region, animated: true)
mapView.delegate = context.coordinator

updateUIView()

Updates the state of the specified view with new information from SwiftUI.

簡單來說就是更新視圖使用
uiView : 當前對象
context : 前後狀態值

這邊的話就是視圖更新時你想讓它觸發什麼
你放個 print 就知道我的意思了


上一篇
第二十日、正題
下一篇
第二十二日、MapKit 篇 - 座位標的設定
系列文
[初探] 用 SwiftUI - 打造一個自助洗衣應用30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言