当应用程序进入和退出后台时暂停和恢复核心动画
我有一个简单的 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
调用
viewWillAppear
可能会导致其他副作用,并且通常不是开始动画的好地方,请尝试监听UIApplicationWillEnterForegroundNotification
并在处理程序。Calling
viewWillAppear
may cause other side effects and is generally not a good place to start animation, try listens toUIApplicationWillEnterForegroundNotification
and add yourresumeLayer:
in the handler.