Remove UIVIew from SuperView with Animation

14,511

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.

Share:
14,511
Keith Fitzgerald
Author by

Keith Fitzgerald

Updated on June 25, 2022

Comments

  • Keith Fitzgerald
    Keith Fitzgerald almost 2 years

    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????

  • Keith Fitzgerald
    Keith Fitzgerald about 15 years
    thanks! i suppose that is my question: how i animate the view show it moves offscreen?
  • Keith Fitzgerald
    Keith Fitzgerald about 15 years
    Ahhhh ... thank man I really appreciate it. so weird the offscreen transition isn't baked in. appreciate the help!