4

我有一个简单的 CABasicAnimation 连接为无限动画(我有一个轮子,我一直在旋转)。这是我设置显式动画的方法:

-(void)perform360rotation:(UIImageView*) imageView {
CABasicAnimation* anim = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
// to rotate around a specific axis, specify it in the KeyPath parameter like below
//CABasicAnimation* anim = [CABasicAnimation animationWithKeyPath:@"transform.rotation.x"];
[anim setDuration:self.speed]; // Animation duration 
[anim setAutoreverses:NO];
[anim setRepeatCount:HUGE_VALF]; // Perfrom animation large number of times
[anim setFromValue:[NSNumber numberWithDouble:0.0f]];
[anim setToValue:[NSNumber numberWithDouble:(M_PI * 2.0f)]];
[[imageView layer] addAnimation:anim forKey:@"wheeloRotation"];
}

我从我的 viewWillAppear 方法中调用了这个动画方法。当应用程序进入后台然后重新出现时,动画不再起作用。谷歌搜索后,我从Apple想出了这个。很好,我在同一个 viewcontroller.m 文件中实现了 Apple 的建议,该文件具有用于旋转视图的 perform360rotation 方法。

-(void)pauseLayer:(CALayer*)layer
{
CFTimeInterval pausedTime = [layer convertTime:CACurrentMediaTime() fromLayer:[self.wheelView layer]];
layer.speed = 0.0;
layer.timeOffset = pausedTime;
NSLog(@"pauseLayer:paused time = %f",pausedTime);
}

-(void)resumeLayer:(CALayer*)layer
{
CFTimeInterval pausedTime = [layer timeOffset];
NSLog(@"resumeLayer:paused time = %f",pausedTime);
layer.speed = 1.0;
layer.timeOffset = 0.0;
layer.beginTime = 0.0;
CFTimeInterval timeSincePause = [layer convertTime:CACurrentMediaTime() fromLayer:[self.wheelView layer]] - pausedTime;
layer.beginTime = timeSincePause;
}

同样,在谷歌上搜索时,我看到普遍的共识是我从 AppDelegate 调用 pause 和 resume 方法。所以我这样做了(lVC 是我在这个问题开头提到的 viewcontroller.m 类。pauseLayer 和 resumeLayer 方法是从其 viewWillAppear 和 viewWillDisappear 方法内部调用的):

- (void)applicationDidEnterBackground:(UIApplication *)application
{
[lVC viewWillDisappear:YES];
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
[lVC viewWillAppear:YES];

}

还没有骰子。当应用重新进入前台时,动画仍然没有恢复。有什么我做错了吗?

4

1 回答 1

1

调用viewWillAppear可能会导致其他副作用,并且通常不是开始动画的好地方,请尝试监听UIApplicationWillEnterForegroundNotification并将您的添加resumeLayer:到处理程序中。

于 2012-02-23T02:23:25.030 回答