![](/img/trans.png)
[英]Understanding the completion block of [UIView animateWithDuration:completion:]
[英]Remove UIView inside the completion block of animateWithDuration:completion
我正在用animateWithDuration:completion為UIView的某些屬性設置動畫。 這些動畫完成后,我想從他的超級視圖中刪除該視圖。 問題是,如果我在完成塊中添加removeFromSuperView,則根本不會獲得動畫。 我發現的解決方法是刪除dispatch_after內部的視圖。 為什么?
var loadingView: Loading! //This is a UIView loaded from a Nib
let delaySecs: NSTimeInterval = 2
UIView.animateWithDuration(delaySecs, animations: { () -> Void in
self.loadingView.cherrysImageView.transform = CGAffineTransformMakeScale(2, 2)
self.loadingView.cherrysImageView.alpha = 0
}) { (succeed) -> Void in
//Can't remove the view here, otherwise i get NO animation
//self.loadingView.removeFromSuperview()
}
//And if i add this instead of removing the view in the completion block, it works as expected
let delay = delaySecs * Double(NSEC_PER_SEC)
let time = dispatch_time(DISPATCH_TIME_NOW, Int64(delay))
dispatch_after(time, dispatch_get_main_queue()) { () -> Void in
self.loadingView.removeFromSuperview()
}
動畫完成塊在動畫過程中被多次調用,但是只有當成功為true時,您才應刪除所有內容。
var loadingView: Loading! //This is a UIView loaded from a Nib
let delaySecs: NSTimeInterval = 2
UIView.animateWithDuration(delaySecs, animations: { () -> Void in
self.loadingView.cherrysImageView.transform = CGAffineTransformMakeScale(2, 2)
self.loadingView.cherrysImageView.alpha = 0
}) { (succeed) -> Void in
if succeed {
self.loadingView.removeFromSuperview()
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.