ios - 删除 animateWithDuration :completion 的完成 block 内的 UIView

标签 ios swift

我正在使用 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/

相关文章:

ios - 如何使用 UITableview 部分的首字母而不是整个部分标签?

swift - 上传到 App Store 时上传失败 ERROR ITMS-90081

ios - 以编程方式滚动到 textView 的底部

ios - iOS 应用程序权限的完整列表

ios - Swift 3 中的 CGRectGetMidX/Y 在哪里

ios - 为什么 "format specifies type id but the argument has type int"崩溃

ios - 为什么我在尝试将 key 保存到钥匙串(keychain)时会返回 errSecParam (-50)?

ios - 当 func 中的归档对象编码时(使用 aCoder : NSCoder) method crashed with swift enum in real revice

ios - PFQueryTableViewController 限制

ios - 为什么 .geocodeAddressString 不改变外部变量?