0

我正在尝试为 a 设置动画,UIImageView并在应用程序最小化(主页按钮、进入另一个应用程序等)并重新输入后让它恢复/启动动画。我在我的viewDidLoad方法中执行以下操作来创建动画、我想要的路径和旋转:

CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, NULL, startX, startY);
CGPathAddCurveToPoint(path, NULL, startX, startY, curvePoint1X, curvePoint1Y, endX, endY);
CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
animation.duration = duration;
animation.path = path;
animation.repeatCount = HUGE_VALF;
[imageView.layer addAnimation:animation forKey:@"theAnimation"];
[UIView animateWithDuration:duration delay:0.0 options:0
                     animations:^{
                         [UIView setAnimationRepeatCount:HUGE_VALF];
                         [UIView setAnimationBeginsFromCurrentState:YES];
                         imageView.transform = CGAffineTransformMakeRotation(M_PI);
                     }
                     completion:NULL];

它工作得很好,但是当我点击主页按钮并返回我的应用程序时,动画没有播放并且我的视图处于原始位置。重新进入应用程序时,如何设置此动画重新开始?我不介意动画是否必须从头开始。我的应用程序也有NSTimer运行,我知道它会恢复,我想在那里运行检查以恢复动画,但我不确定这是最好的解决方案。

4

1 回答 1

0

对于任何感兴趣的人,我想出了一个解决我的问题的方法。我有一个“NSTimer”正在运行,并通过查看表示层框架定期检查我的视图是否正在移动,如果动画没有移动,我会再次启动动画。检查表示层框架是棘手的部分,我这样做了:

-(BOOL) isViewNotMoving:(UIImageView *) imageView {
  // Grab values from view presentation layer and compare to test movement
  CGRect viewLocation = [[[imageView layer] presentationLayer] frame];
  double currentX = viewLocation.origin.x;
  double currentY = viewLocation.origin.y;
  BOOL testResult = false;
  if ((currentX - previousX == 0) && (currentY - previousY == 0)) {
      testResult = true;
  } else {
      testResult = false;
  }
  previousX = currentX;
  previousY = currentY;

  return testResult;
}

在哪里previousXpreviousY之前声明过。希望其他人觉得这很有用。

于 2012-09-05T17:01:42.733 回答