当应用程序进入和退出后台时暂停和恢复核心动画

发布于 2024-12-29 22:11:50 字数 2060 浏览 2 评论 0原文

我有一个简单的 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 方法调用此动画方法。当应用程序进入后台然后重新出现时,动画不再起作用。 经过谷歌搜索,我从苹果公司找到了这个。一切都很好,我在同一个 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 调用暂停和恢复方法。所以我这样做了(lVC是我在这个问题开始时提到的viewcontroller.m类。pauseLayer和resumeLayer方法是从其viewWillAppear和viewWillDisappear方法内部调用的):

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

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

}

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

I have a simple CABasicAnimation wired up as an infinite animation (I have a wheel that I keep rotating forever). Here is the method where I setup that explicit animation:

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

I call this animtion method from my viewWillAppear method. When the app enters background and then re-appears, the animation does not Work anymore.
Upon googling, I came up with this from Apple. All fine, I implemented Apple's recommendations like this in the same viewcontroller.m file that has the perform360rotation method for rotating the view.

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

Again, on googling, I saw that the general consensus was that I call the pause and resume methods from the AppDelegate. So I did them like this (lVC is the viewcontroller.m class that has I mentioned at the start of this question. The pauseLayer and resumeLayer methods are being called from inside its viewWillAppear and viewWillDisappear methods):

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

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

}

No dice yet. The animation still does not resume when app re-enters foreground. Is there something that I am doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

放低过去 2025-01-05 22:11:50

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

Calling viewWillAppear may cause other side effects and is generally not a good place to start animation, try listens to UIApplicationWillEnterForegroundNotification and add your resumeLayer: in the handler.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文