Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove UIVIew from SuperView with Animation

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

like image 894
Keith Fitzgerald Avatar asked Mar 09 '09 21:03

Keith Fitzgerald


People also ask

Does UIView animate need weak self?

You don't need to use [weak self] in static function UIView. animate() You need to use weak when retain cycle is possible and animations block is not retained by self.

Does UIView animate run on the main thread?

The contents of your block are performed on the main thread regardless of where you call [UIView animateWithDuration:animations:] . It's best to let the OS run your animations; the animation thread does not block the main thread -- only the animation block itself.


1 Answers

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.

like image 132
Andrew Grant Avatar answered Oct 03 '22 01:10

Andrew Grant