Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIView animation completion callback?

Can I setup a function to be called once my animation is complete? I want to fade a UIView and then remove it from the superView.

like image 421
Slee Avatar asked Jul 02 '10 14:07

Slee


People also ask

What is withAnimation SwiftUI?

withAnimation() takes a parameter specifying the kind of animation you want, so you could create a three-second linear animation like this: withAnimation(.linear(duration: 3)) Explicit animations are often helpful because they cause every affected view to animate, not just those that have implicit animations attached.

How do you animate a view in Swift?

To be exact, whenever you want to animate the view, you actually call layoutIfNeeded on the superview of that view. Try this instead: UIView. animate(withDuration: 0.1, delay: 0.1, options: UIViewAnimationOptions.

How do I add animations to SwiftUI?

SwiftUI has built-in support for animations with its animation() modifier. To use this modifier, place it after any other modifiers for your views, tell it what kind of animation you want, and also make sure you attach it to a particular value so the animation triggers only when that specific value changes.


2 Answers

Animation blocks were introduced in iOS4. Apple recommends you use these, and the new methods mostly ask for completion blocks which replace callbacks. For example:

[UIView animateWithDuration:0.5f
                      delay:0.0f
                    options:UIViewAnimationCurveEaseInOut
                 animations:^{
                   [myView setAlpha:0.0f];
                 }
                 completion:^(BOOL finished) {
                   [myView removeFromSuperview];
                 }]; 
like image 86
SK9 Avatar answered Sep 29 '22 16:09

SK9


Yes, that's easy:

When you configure your animation

[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(myAnimationStopped:finished:context:)];

And define your method like:

-(void)myAnimationStopped:(NSString *)animationID 
                 finished:(NSNumber *)finished
                  context:(void *)context {
   // fancy code here
}

Doesn't have to be self and that method, of course.

like image 38
Eiko Avatar answered Sep 29 '22 15:09

Eiko