Remove UIVIew from SuperView with Animation

2019-03-10 07:09发布

问题:

I can animate the addition of a UIView to my app, it looks very pretty so thank you apple.

However, how do I animate the removal of this view from the super view?

I'm using:

CATransition *animation = [CATransition animation];
[animation setDuration:1];
[animation setType:kCATransitionReveal];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]];
[[myview layer] addAnimation:animation forKey:kCATransitionReveal];

to animate the "in" transition ... how do you animate the "out" transition????

回答1:

Animate your view so it moves offscreen/shrinks/expands/fades, then do the actual removal when the animation ends.

You can do this by altering the properties of the view (position/size/offset) between a beginAnimations/commitAnimations block. UIKit will then animate these properties over the time specified.

E.g something like;

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.30f];
view.transform = 
  CGAffineTransformMakeTranslation(
    view.frame.origin.x, 
    480.0f + (view.frame.size.height/2)  // move the whole view offscreen
  );
background.alpha = 0; // also fade to transparent
[UIView commitAnimations];

In the animation end notification you can then remove the view.