是否可以使用KVO跟踪对“MKMapView.camera.heading”属性的更改?

时间:2016-11-20 10:21:02

标签: ios mapkit key-value-observing

我正在尝试使用相机标题属性跟踪地图方向的更改。

// 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)
    }
}

1 个答案:

答案 0 :(得分:0)

摄影机属性模式为复制,因此您不会仅在调用getter时观察到当前摄影机实例。您需要在地图视图上观察@"camera.heading"@"camera"的关键路径,并希望在航向更改时在内部设置新的相机对象。