我如何在MKMapKit中旋转地图后自动旋转注释视图图像?

时间:2019-12-17 14:09:20

标签: swift mapkit swiftui

地图旋转时,我的代码确实旋转annotationViewImage

 func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
      let annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "mark")
      annotationView.image =  UIImage(named: "pinArrow")
      let ang = mapView.camera.heading
      let radi = Double.pi * Double(ang / 360)
      let resultAng = 2 * Float(radi)
      annotationView.transform = CGAffineTransform(rotationAngle: CGFloat(-resultAng))
      annotationView.canShowCallout = true
              return annotationView
}

以上代码,当地图旋转时,不实现上述方法。 因此,添加以下代码

func mapViewDidChangeVisibleRegion(_ mapView: MKMapView) {
    print("visible")
    let anns = mapView.annotations
    mapView.removeAnnotations(anns)
    mapView.addAnnotations(anns)
}

,然后,当地图旋转时,annotationView图像也会旋转。 但是这么慢一些,而加载时间又长一些。 因为上面代码的地图移动委托,所以很多重复。

我寻求一种更简单的方法,而不会缓慢加载。 如何旋转同步地图和annotationView图片?

0 个答案:

没有答案
相关问题