2016-07-24 86 views
0

我在Playground中擺弄我的MKMapView,我無法動畫它的frame.size.height。這是我的遊樂場代碼:MKMapView不遵守動畫延遲

let vc = UIViewController() 
vc.view.backgroundColor = UIColor.whiteColor() 
XCPlaygroundPage.currentPage.liveView = vc 

let map = MKMapView(frame: vc.view.frame) 
map.backgroundColor = UIColor.redColor() 
map.autoresizesSubviews = false 
vc.view.addSubview(map) 

UIView.animateWithDuration(2.0, delay: 1.0, usingSpringWithDamping: 0.5, initialSpringVelocity: 1.0, options: .CurveEaseInOut, animations: { 

    map.frame.size.height = 200 

    }, completion: nil) 

您可以通過觀察地圖的紅色背景顏色來判斷實際動畫何時發生。可見地圖的高度在MKMapView的高度之前下降。設置autoresizesSubviews = false似乎沒有做任何事情。那麼爲什麼看起來好像有兩個不同的動畫呢?

UPDATE在我的遊樂場,我打消了我所有的MKMapView子視圖,以及_MKMapContentView,一個地圖的兩個子視圖的(另一個是「法律」 MKAttributionLabel),已被刪除,只有它是紅色的背景渲染MKMapView 。所以_MKMapContentView被調整爲子視圖,但調用map.autoresizesSubviews = false沒有做到這一點。是什麼賦予了?

enter image description here

回答

0

我想你也許對用戶是爲了避免:

map.backgroundColor = UIColor.whiteColor() 

vc.view.addSubview(map) 

UIView.animateWithDuration(2.0, delay: 1.0, usingSpringWithDamping: 0.5, initialSpringVelocity: 1.0, options: .CurveEaseInOut, animations: { 

    map.frame.size.height = 200 

    }, completion: 
    map.backgroundColor = UIColor.redColor() 
) 
+0

謝謝,但那不是我問的。我只是使用紅色背景作爲例子。我問爲什麼看起來好像有兩個動畫。 – chicobermuda

0

這是一個黑客位的,如果你願意。我嘗試使用animateWithDuration:delay:usingSpringWithDamping和非零延遲動畫地圖的高度。我的問題是,_MKMapContentView不會堅持延遲,這是解釋不同的動畫。

所以,在@ Matt的famous delay solution的幫助下,我放棄了我的函數延遲,並將整個方法放在延遲塊中,這樣任何(子)視圖都會同時進行動畫處理。

delay(3.5) { 

    UIView.animateWithDuration(1.0, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 1.0, options: .CurveEaseInOut, animations: { 

     map.frame.size.height -= 200 

     }, completion: nil) 

} 


func delay(delay:Double, closure:()->()) { 
    dispatch_after(
     dispatch_time(
      DISPATCH_TIME_NOW, 
      Int64(delay * Double(NSEC_PER_SEC)) 
     ), 
     dispatch_get_main_queue(), closure) 
} 

位變通方法,但它的工作原理。仍然渴望瞭解爲什麼地圖的子視圖不符合函數延遲,如果任何人有想法。