Perform an action after the animation has finished

大兔子大兔子 提交于 2019-12-22 18:33:09

问题


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];

}

回答1:


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];
    }];


来源:https://stackoverflow.com/questions/17686840/perform-an-action-after-the-animation-has-finished

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!