[英]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.