我正在尝试使用相机标题属性跟踪地图方向的更改。
// 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)
}
}
答案 0 :(得分:0)
摄影机属性模式为复制,因此您不会仅在调用getter时观察到当前摄影机实例。您需要在地图视图上观察@"camera.heading"
或@"camera"
的关键路径,并希望在航向更改时在内部设置新的相机对象。