简体   繁体   中英

Remove UIView inside the completion block of animateWithDuration:completion

I'm animating some properties of a UIView with animateWithDuration:completion. After those animations are complete i want to remove this view from his superview. The problem is that if i add inside the completion block removeFromSuperView i dont get animations at all. The workaround i found is to remove the view inside dispatch_after. Why?

     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()
    }

The animation completion block is called multiple times during the animation, but only when succeed is true should you remove everything.

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()
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM