2016-07-24 32 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) 
} 

位变通方法,但它的工作原理。仍然渴望了解为什么地图的子视图不符合函数延迟,如果任何人有想法。