Anil
Anil

Reputation: 2438

Issue with UIView animation block

I'm trying to implement a nested animation block. The issue I'm facing is that only my completion block code is called. When I comment that out, the initial part is called. How do I get the animation to finish the first animation and then call the second one?

- (void)viewDidAppear:(BOOL)animated
{
    _finalLogoCenter = self.logoImage.center;
    // Logo Animation
    [self logoAnimation];
}

- (void) logoAnimation
{

    [UIView animateWithDuration:1.0
                          delay:0.0
                        options:UIViewAnimationOptionTransitionNone
                     animations:^ {
                         // Move Up
                         [self moveLogoUpFirst];
                     }completion:^(BOOL finished)
                    {
                         // Move below final point
                         [self moveLogoDownBeyondFinalPoint];
                    }];
}

- (void) moveLogoUpFirst
{
    __block CGPoint logoCenter = self.logoImage.center;
    logoCenter.y = 290.0f;
    self.logoImage.center = logoCenter;
    [self.logoImage setAlpha:0.0f];
    [UIView animateWithDuration:1.0
                          delay:0.0
                        options:UIViewAnimationOptionTransitionNone
                     animations:^ {
                         logoCenter.y = _finalLogoCenter.y - 20;
                         self.logoImage.center = logoCenter;
                         [self.logoImage setAlpha:1.0f];
                     }completion:nil];
}

- (void) moveLogoDownBeyondFinalPoint
{
    __block CGPoint logoCenter = self.logoImage.center;
    self.logoImage.center = logoCenter;
    [UIView animateWithDuration:1.0
                          delay:0.0
                        options:UIViewAnimationOptionTransitionNone
                     animations:^ {
                         logoCenter.y = _finalLogoCenter.y + 5;
                         self.logoImage.center = logoCenter;
                     }completion:nil];
}

Upvotes: 0

Views: 305

Answers (2)

streem
streem

Reputation: 9144

I believe the problem comes from an animation which is inside another animation. That does not make any sense and i guess it could lead to unexpected behavior.

What you could do is set a completion block in the first animation

- (void) moveLogoUpFirst:(void (^)(BOOL finished))completion
{
    __block CGPoint logoCenter = self.logoImage.center;
    logoCenter.y = 290.0f;
    self.logoImage.center = logoCenter;
    [self.logoImage setAlpha:0.0f];
    [UIView animateWithDuration:1.0
                          delay:0.0
                        options:UIViewAnimationOptionTransitionNone
                     animations:^ {
                         logoCenter.y = _finalLogoCenter.y - 20;
                         self.logoImage.center = logoCenter;
                         [self.logoImage setAlpha:1.0f];
                     }completion:^(BOOL finished)
                    {
                         completion(finished);
                    }];
}

And when you call it just use this code without any animation :

[self moveLogoUpFirst:^(BOOL finished) {
    [self moveLogoDownBeyondFinalPoint]
}];

Upvotes: 1

Calin Chitu
Calin Chitu

Reputation: 1099

As a general approach, you can create a function like this:

-(void)someFunctionWithCompletion:(void (^)(void))completionBlock{
    [UIView animateWithDuration:1.0
                          delay:0.0
                        options:UIViewAnimationOptionTransitionNone
                     animations:^ {
                     }completion:^(BOOL finished)
                     {
                       if(completionBlock)
                          completionBlock();
                     }];
}

And call it:

[... someFunctionWithCompletion:^{
}];

Upvotes: 0

Related Questions