简体   繁体   English

当应用进入和退出背景时暂停和恢复核心动画

[英]pausing and resuming core animation when app enters and exits background

I have a simple CABasicAnimation wired up as an infinite animation (I have a wheel that I keep rotating forever). 我有一个简单的CABasicAnimation连接成无限动画(我有一个轮子,我一直在旋转)。 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. 我从viewWillAppear方法中调用了这个animtion方法。 When the app enters background and then re-appears, the animation does not Work anymore. 当应用程序进入后台然后重新出现时,动画将不再起作用。 Upon googling, I came up with this from Apple. 谷歌搜索时,我从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. 一切都很好,我在同一个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;
}

Again, on googling, I saw that the general consensus was that I call the pause and resume methods from the AppDelegate. 再次,在谷歌搜索,我看到普遍的共识是我从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): 所以我这样做了(lVC是我在本期问题开头提到过的viewcontroller.m类。从viewWillAppear和viewWillDisappear方法中调用pauseLayer和resumeLayer方法):

- (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? 有什么我做错了吗?

调用viewWillAppear可能会导致其他副作用,并且通常不是启动动画的好地方,尝试侦听UIApplicationWillEnterForegroundNotification并在处理程序中添加resumeLayer: .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从后台恢复应用程序时,RestKit崩溃 - RestKit crash when resuming the app from background 当应用程序从后台恢复时,活动指示器动画会停止 - Activity Indicator Animation Ceases upon App Resuming From Background 应用程序进入后台时取消任务 - Task Cancellation when app enters background 应用进入后台时如何获取本地通知 - How to get local notifications when app enters the background iPhone:为什么当应用程序进入后台时,applicationMusicPlayer为什么会退出播放? - iPhone: Why does applicationMusicPlayer quit playing when app enters background? 当应用程序进入后台时,popToRootViewController - popToRootViewController when application enters background 恢复应用程序时获取launchOptions - Get launchOptions when resuming App 在iPhone中创建递归后台处理程序是否合法?(当应用进入后台时进行处理) - Is it legal to create a recursive background handler in iphone?(processing when app enters the background) 当应用程序进入后台/活动状态时,安全地暂停/恢复远程UIWebView WebSockets - Safely Pause/Resume Remote UIWebView WebSockets when the app enters Background/Active states 当我的应用进入后台时,如何停止Google Analytics(分析)跟踪会话? - How can I stop Google analytics tracking session when my app enters into background?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM