在animateWithDuration的完成块中删除UIView:完成

时间:2015-05-05 15:18:49

标签: ios swift

我正在使用animateWithDuration:完成来动画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()
    }

1 个答案:

答案 0 :(得分:1)

在动画期间多次调用动画完成块,但只有当成功为真时才应删除所有内容。

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()
    }
}
相关问题