2017-06-21 121 views
0

我試圖做一些花哨的旋轉(帶間距)的MapKit動畫。有了這個代碼的iOS - MapKit相機旋轉動畫

func animateCamera() { 
     let newCamera: MKMapCamera? = mapView.camera 
      newCamera?.pitch = 90.0 
      newCamera?.heading = 180.0 
      newCamera?.altitude = 200.0 

     let camera: MKMapCamera? = mapView.camera 
      camera?.pitch = 0.0 
      camera?.heading = 360.0 
      camera?.altitude = 20.0 

     UIView.animate(withDuration: 3.0, delay: 0.0, options: ([.curveLinear, .beginFromCurrentState]), animations: {() -> Void in 
      self.mapView.setCamera(newCamera!, animated: true); 
     }, completion: {(_ finished: Bool) -> Void in 
      print("has not been interrupted : \(finished)") 
     }) 

     UIView.animate(withDuration: 3.0, delay: 3.0, options: ([.curveLinear, .beginFromCurrentState]), animations: {() -> Void in 
      self.mapView.setCamera(camera!, animated: true); 
     }, completion: {(_ finished: Bool) -> Void in 
      print("has not been interrupted : \(finished)") 
     }) 
} 

但是當我運行它只是捕捉到給定航向,俯仰和高度WITHOUT動畫功能。有什麼我錯過或做錯了?

在此先感謝!

回答

1

我知道,必須有更好的方式來做到這一點,但是這是我發現的方式,工作時,主要的問題是當動畫結束就知道了,我沒有發現任何線索吧,所以我使用2個秒鐘任意時間

func animateCamera() { 
    let newCamera: MKMapCamera = MKMapCamera(lookingAtCenter: self.mapView.camera.centerCoordinate, fromDistance: 200.0, pitch: 90.0, heading: 180.0) 
    self.mapView.setCamera(newCamera, animated: true) 

    DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + Double(Int64(2 * Double(NSEC_PER_SEC)))/Double(NSEC_PER_SEC)) { 
     let newCamera: MKMapCamera = MKMapCamera(lookingAtCenter: self.mapView.camera.centerCoordinate, fromDistance: 20.0, pitch: 0.0, heading: 360.0) 
     self.mapView.setCamera(newCamera, animated: true) 
    } 
} 

希望這有助於你

+0

謝謝Reinier! –