【问题标题】:Perform an action after the animation has finished动画完成后执行动作
【发布时间】:2013-07-16 20:57:17
【问题描述】:

我正在对图像执行动画,并将其旋转 180 度。

然后我插入了一个代码来切换到另一个视图。

但是,应用会在动画结束前切换视图。

如何等待动画完成才能切换视图?

这是我的代码:

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

}

【问题讨论】:

  • 我从另一篇文章中找到了答案。它使用以下内容:[self performSelector:@selector(metohodToPushView) withObject:nil afterDelay:0.8];
  • 这不是实现这一目标的好方法。使用 performSelector:withObject:afterDelay: 是一种技巧。查看有关 animateWithDuration:animations:completion: 的答案。
  • ... 和 CGAffineTransformRotate 可用于旋转视图的 transform 属性。

标签: ios animation switch-statement wait


【解决方案1】:

使用 UIView 的 animateWithDuration:animations:completion: 方法。在完成块中调用 performSegueWithIdentifier:sender:。例如:

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

【讨论】:

  • 我试过了,但它没有等待 1.5 秒。它直接切换视图。我在第一篇文章中添加了整个动画代码,直到切换视图。
  • 你是在里面添加 CABasicAnimation 代码吗?这不应该是必要的,它甚至可能会干扰 animateWithDuration。请参阅我给出的示例中的更新代码,了解如何使用 animateWithDuration 进行旋转变换。
猜你喜欢
  • 2012-06-22
  • 1970-01-01
  • 1970-01-01
  • 2013-08-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多