【发布时间】:2012-06-26 11:21:49
【问题描述】:
我有一个基本动画,只是旋转图像,我正在使用核心动画。在 viewWillAppear 方法中,我创建了动画:
gearRotate = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
gearRotate.repeatCount = HUGE_VALF;
gearRotate.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
gearRotate.duration = 2;
gearRotate.fromValue = 0;
gearRotate.toValue = [NSNumber numberWithFloat:(360*M_PI)/180];
[gearButton.imageView.layer addAnimation:gearRotate forKey:@"transform.rotation"];
[self stopGear];
我最初调用 stopGear 是为了暂停动画。我确实在 viewDidLoad 中有这段代码。无论如何,当动画被调用时它工作正常:
-(void)startGear {
CFTimeInterval pausedTime = [gearButton.imageView.layer timeOffset];
gearButton.imageView.layer.speed = 1.0;
gearButton.imageView.layer.timeOffset = 0.0;
gearButton.imageView.layer.beginTime = 0.0;
CFTimeInterval timeSincePause = [gearButton.imageView.layer convertTime:CACurrentMediaTime() fromLayer:nil] - pausedTime;
gearButton.imageView.layer.beginTime = timeSincePause;
}
这个暂停方法也很好用:
-(void)stopGear {
CFTimeInterval pausedTime = [gearButton.imageView.layer convertTime:CACurrentMediaTime() fromLayer:nil];
gearButton.imageView.layer.speed = 0.0;
gearButton.imageView.layer.timeOffset = pausedTime;
}
但是,如果我转到另一个视图然后返回主视图,这一切都会失败。该应用程序运行良好,但如果我去过另一个视图,动画将永远不会运行。
这就是我最初将此代码移动到 viewWillAppear 的原因,我认为我需要重新创建动画,但没有 bean。有什么想法吗?
谢谢。
【问题讨论】:
-
startGear什么时候在你返回时运行? -
startGear 在用户每次按下按钮时被调用。我调用 [self startGear] 和 [self lowerGUI] - lowerGUI 方法工作正常(它是 UIView 动画)但 CA 不运行。
-
如果你在启动之前检查图层的
animationKeys,你的齿轮是否在图层上旋转? -
其实我只是说
if ([[gearButton.imageView layer] animationForKey:@"transform.rotation"]) { // there is a animation to be modified}回到视图上面的代码是按什么顺序运行的?
标签: ios5 core-animation segue