Note that there are some explanatory texts on larger screens.

plurals
  1. POpausing and resuming core animation when app enters and exits background
    primarykey
    data
    text
    <p>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:</p> <pre><code>-(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"]; } </code></pre> <p>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 <a href="http://developer.apple.com/library/ios/#qa/qa1673/_index.html" rel="nofollow">this</a> 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.</p> <pre><code>-(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; } </code></pre> <p>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):</p> <pre><code>- (void)applicationDidEnterBackground:(UIApplication *)application { [lVC viewWillDisappear:YES]; } - (void)applicationWillEnterForeground:(UIApplication *)application { [lVC viewWillAppear:YES]; } </code></pre> <p>No dice yet. The animation still does not resume when app re-enters foreground. Is there something that I am doing wrong?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload