簡體   English   中英

是否可以使用 KVO 來跟蹤對 `MKMapView.camera.heading` 屬性的更改?

[英]Is it possible to use KVO to track changes to the `MKMapView.camera.heading` property?

我正在嘗試使用相機航向屬性跟蹤地圖方向的變化。

// Register for notifications of changes to camera
if let camera = self.mapView?.camera {
    self.camera = camera
    camera.addObserver(self, forKeyPath: "heading", options: NSKeyValueObservingOptions.new, context: &MapViewController.myContext)
}

...

override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
    if context == &MapViewController.myContext {
        if keyPath == "heading" {
            if let heading = change?[NSKeyValueChangeKey.newKey] as? CLLocationDirection {
                self.heading = heading
            }
        }
    } else {
        super.observeValue(forKeyPath: keyPath, of: object, change: change, context: context)
    }
}

相機屬性模式是復制,因此您不會觀察當前的相機實例,而只是在調用 getter 時復制的實例。 您需要在地圖視圖上觀察@"camera.heading"@"camera"關鍵路徑,並希望在航向改變時內部設置新的相機對象。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM