Skip to content

Commit

Permalink
Fix issue with animation behaviour when sending the app to the backgr…
Browse files Browse the repository at this point in the history
…ound

For living animation objects (usually retained animation objects) which are not running, the resume mechanism was leading to the animation still being played
  • Loading branch information
defagos committed Dec 21, 2012
1 parent 2943087 commit f282241
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 18 deletions.
1 change: 1 addition & 0 deletions CoconutKit/Sources/Animation/HLSAnimation.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
NSUInteger m_currentRepeatCount;
NSTimeInterval m_remainingTimeBeforeStart; // the time remaining before the start time is reached
NSTimeInterval m_elapsedTime; // the currently elapsed time (does not include pauses)
BOOL m_runningBeforeEnteringBackground; // was the animation running before the application entered background?
BOOL m_pausedBeforeEnteringBackground; // was the animation paused before the application entered background?
BOOL m_running;
BOOL m_playing;
Expand Down
45 changes: 27 additions & 18 deletions CoconutKit/Sources/Animation/HLSAnimation.m
Original file line number Diff line number Diff line change
Expand Up @@ -582,28 +582,37 @@ - (id)copyWithZone:(NSZone *)zone

- (void)applicationDidEnterBackground:(NSNotification *)notification
{
// Core Animations are removed and added back again automatically when the application enters / leaves background. This makes
// it very hard to resume running animations after the application wakes up, since we have no real control over this process.
// But since HLSAnimations can be given a start time, we can apply the following strategy to solve those issues:
// 1) Remember how much time has elapsed and cancel the animation when the application enters background. The remaining
// delegate events are not received
// 2) Rewind the animation at the beginning, without a delegate
// 3) Play the animation from where it was cancelled when the application enters foreground
m_elapsedTime += [self.currentAnimationStep elapsedTime];
m_pausedBeforeEnteringBackground = self.paused;

[self cancel];

HLSAnimation *reverseAnimation = [self reverseAnimation];
reverseAnimation.delegate = nil;
[reverseAnimation playAnimated:NO];
m_runningBeforeEnteringBackground = self.running;

if (m_runningBeforeEnteringBackground) {
// Core Animations are removed and added back again automatically when the application enters / leaves background. This makes
// it very hard to resume running animations after the application wakes up, since we have no real control over this process.
// But since HLSAnimations can be given a start time, we can apply the following strategy to solve those issues:
// 1) Remember how much time has elapsed and cancel the animation when the application enters background. The remaining
// delegate events are not received
// 2) Rewind the animation at the beginning, without a delegate
// 3) Play the animation from where it was cancelled when the application enters foreground
m_elapsedTime += [self.currentAnimationStep elapsedTime];
m_pausedBeforeEnteringBackground = self.paused;

[self cancel];

HLSAnimation *reverseAnimation = [self reverseAnimation];
reverseAnimation.delegate = nil;
[reverseAnimation playAnimated:NO];
}
}

- (void)applicationWillEnterForeground:(NSNotification *)notification
{
[self playWithStartTime:m_elapsedTime repeatCount:m_repeatCount];
if (m_pausedBeforeEnteringBackground) {
[self pause];
if (m_runningBeforeEnteringBackground) {
[self playWithStartTime:m_elapsedTime repeatCount:m_repeatCount];
if (m_pausedBeforeEnteringBackground) {
[self pause];
}

m_runningBeforeEnteringBackground = NO;
m_pausedBeforeEnteringBackground = NO;
}
}

Expand Down

0 comments on commit f282241

Please sign in to comment.