我正在使用 animateWithDuration:completion 为 UIView 的一些属性设置动画。这些动画完成后,我想从他的 super View 中删除这个 View 。 问题是,如果我在完成 block removeFromSuperView 中添加,我根本得不到动画。我找到的解决方法是删除 dispatch_after 中的 View 。为什么?
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()
}
最佳答案
动画完成过程中会多次调用动画完成 block ,但只有当 success 为 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()
}
}
关于ios - 删除 animateWithDuration :completion 的完成 block 内的 UIView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30057011/