firewall
firewall

Reputation: 638

Perform an action after the animation has finished

I'm performing an animation on an image where I rotate it 180 degrees.

Then I have inserted a code to switch to another view.

However, the app is switching views before the animation ends.

How can I wait for the animation to finish in order to switch view afterwards?

Here's my code:

- (IBAction)buttonPressed:(id)sender
{
    CABasicAnimation *imageRotation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];

    imageRotation.fromValue = [NSNumber numberWithFloat:0];
    imageRotation.toValue = [NSNumber numberWithFloat:((180*M_PI)/ 180)];

    imageRotation.duration = 1.5;
    imageRotation.repeatCount = 1;

    imageRotation.removedOnCompletion = NO;
    imageRotation.autoreverses=NO;
    imageRotation.fillMode = kCAFillModeForwards;

    [_image.layer addAnimation:imageRotation forKey:@"imageRotation"];

    // Switching Views
    [self performSegueWithIdentifier: @"ToGalleryVCSegue" sender: self];

}

Upvotes: 2

Views: 959

Answers (1)

Nicholas Hart
Nicholas Hart

Reputation: 1724

Use UIView's animateWithDuration:animations:completion: method. Put your call to performSegueWithIdentifier:sender: in the completion block. eg:

[UIView animateWithDuration:1.5
                 animations:^{
        // put your animation code here, eg:
        CGAffineTransform transform = CGAffineTransformMakeRotation(M_PI_2);
        self.imageView.transform = transform;
    }
                 completion:^{
        [self performSegueWithIdentifier: @"ToGalleryVCSegue" sender: self];
    }];

Upvotes: 5

Related Questions