Starting another animation after the first end of the animation (Objective-C)

I have a simple animation that just changes the position of the button:

[UIView beginAnimation:nil context:nil];
[UIView setAnimationsDuration:3.0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
mybutton.frame = CGREctMake(20, 233, 280, 46);
[UIView commitAnimations];

I want to do some other animations when this one is finished, how to do it?

+3
source share
4 answers

code segment to start with. setup initial (first) animation:

- (void) aniPath:(AnimationPath *) path
{   
    [UIView beginAnimations:path->aniname context:path];
    [UIView setAnimationDuration:path->interval];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(aniDone:finished:context:)];

    // your initial animation

    [UIView commitAnimations];
}

to another animation when the first one is executed:

- (void) aniDone:(NSString *) aniname finished:(BOOL) finished context:(void *) context
{
    AnimationPath *path = (AnimationPath *) context;
    if ([aniname isEqualToString:path->aniname]) {
        [UIView beginAnimations:path->aniname context:context];
        [UIView setAnimationDuration:path->interval];
        [UIView setAnimationDelegate:self];
        [UIView setAnimationDidStopSelector:@selector(aniDone:finished:context:)];

        // more animations, even recursively 

        [UIView commitAnimations];          
    }
}
+1
source

setAnimationBeginsFromCurrentState:. , [UIView commitAnimations] . , , , , , . :

[UIView beginAnimation:nil context:nil];
// set up the first animation
[UIView commitAnimations];

[UIView beginAnimation:nil context:nil];
[UIView setAnimationBeginsFromCurrentState:NO];
// set up the second animation
[UIView commitAnimations];

, -

[UIView beginAnimation:nil context:nil];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)
// set up the first animation
[UIView commitAnimations];

//...

- (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
    // set up the second animation here
}
+5

+ [UIVIew setAnimationDelegate:] + [UIView setAnimationDidStopSelector:], , .

+1

.

you can just

- (void)animationDidContiune:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
    UIView *currentObj = context;
    [self callTheAnimation: currentObj];
}

You can easily loop the animation.

To finish it - I would create a DidEnded animation and use it instead of the DidContiune animation when you want to stop it (for example, simply if the animation that selects either contiune or end).

HF

AND WHAT IS IMPORTANT:

use this type of animation methods:

- (void)callTheAnimation:(UIView*)itemView
{

    [UIView beginAnimations:@"animation" context:itemView];
        .
        .
        .
}

they are quite flexible - you can animate everything here because of the hierarchy of objects.

+1
source

Source: https://habr.com/ru/post/1742744/


All Articles