Skip to content

Instantly share code, notes, and snippets.

@matej
Last active February 16, 2023 18:43
Show Gist options
  • Save matej/9639064 to your computer and use it in GitHub Desktop.
Save matej/9639064 to your computer and use it in GitHub Desktop.
Persists (pauses) layer animations (including UIView animation generated animations) when the application enters into background and restores (resumes) animations from where they left off upon returning from background.
//
// CALayer+MBAnimationPersistence.h
//
// Created by Matej Bukovinski on 19. 03. 14.
// Copyright (c) 2014 Matej Bukovinski. All rights reserved.
//
#import <QuartzCore/QuartzCore.h>
@interface CALayer (MBAnimationPersistence)
/**
Animation keys for animations that should be persisted.
Inspect the `animationKeys` array to find valid keys for your layer.
`CAAnimation` instances associated with the provided keys will be copied and held onto,
when the applications enters background mode and restored when exiting background mode.
Set to `nil`to disable persistance.
*/
@property (nonatomic, strong) NSArray *MB_persistentAnimationKeys;
/** Set all current `animationKeys` as persistent. */
- (void)MB_setCurrentAnimationsPersistent;
@end
//
// CALayer+MBAnimationPersistence.m
//
// Created by Matej Bukovinski on 19. 03. 14.
// Copyright (c) 2014 Guerrilla Code. All rights reserved.
//
#import <objc/runtime.h>
#import "CALayer+MBAnimationPersistence.h"
@interface MBPersistentAnimationContainer : NSObject
@property (nonatomic, weak) CALayer *layer;
@property (nonatomic, copy) NSArray *persistentAnimationKeys;
@property (nonatomic, copy) NSDictionary *persistedAnimations;
- (id)initWithLayer:(CALayer *)layer;
@end
@interface CALayer (MBAnimationPersistencePrivate)
@property (nonatomic, strong) MBPersistentAnimationContainer *MB_animationContainer;
@end
@implementation CALayer (MBAnimationPersistence)
#pragma mark - Public
- (NSArray *)MB_persistentAnimationKeys {
return self.MB_animationContainer.persistentAnimationKeys;
}
- (void)setMB_persistentAnimationKeys:(NSArray *)persistentAnimationKeys {
MBPersistentAnimationContainer *container = [self MB_animationContainer];
if (!container) {
container = [[MBPersistentAnimationContainer alloc] initWithLayer:self];
[self MB_setAnimationContainer:container];
}
container.persistentAnimationKeys = persistentAnimationKeys;
}
- (void)MB_setCurrentAnimationsPersistent {
self.MB_persistentAnimationKeys = [self animationKeys];
}
#pragma mark - Associated objects
- (void)MB_setAnimationContainer:(MBPersistentAnimationContainer *)animationContainer {
objc_setAssociatedObject(self, @selector(MB_animationContainer), animationContainer, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (MBPersistentAnimationContainer *)MB_animationContainer {
return objc_getAssociatedObject(self, @selector(MB_animationContainer));
}
#pragma mark - Pause and resume
// TechNote QA1673 - How to pause the animation of a layer tree
// @see https://developer.apple.com/library/ios/qa/qa1673/_index.html
- (void)MB_pauseLayer {
CFTimeInterval pausedTime = [self convertTime:CACurrentMediaTime() fromLayer:nil];
self.speed = 0.0;
self.timeOffset = pausedTime;
}
- (void)MB_resumeLayer {
CFTimeInterval pausedTime = [self timeOffset];
self.speed = 1.0;
self.timeOffset = 0.0;
self.beginTime = 0.0;
CFTimeInterval timeSincePause = [self convertTime:CACurrentMediaTime() fromLayer:nil] - pausedTime;
self.beginTime = timeSincePause;
}
@end
@implementation MBPersistentAnimationContainer
#pragma mark - Lifecycle
- (id)initWithLayer:(CALayer *)layer {
self = [super init];
if (self) {
_layer = layer;
}
return self;
}
- (void)dealloc {
[self unregisterFromAppStateNotifications];
}
#pragma mark - Keys
- (void)setPersistentAnimationKeys:(NSArray *)persistentAnimationKeys {
if (persistentAnimationKeys != _persistentAnimationKeys) {
if (!_persistentAnimationKeys) {
[self registerForAppStateNotifications];
} else if (!persistentAnimationKeys) {
[self unregisterFromAppStateNotifications];
}
_persistentAnimationKeys = persistentAnimationKeys;
}
}
#pragma mark - Persistence
- (void)persistLayerAnimationsAndPause {
CALayer *layer = self.layer;
if (!layer) {
return;
}
NSMutableDictionary *animations = [NSMutableDictionary new];
for (NSString *key in self.persistentAnimationKeys) {
CAAnimation *animation = [layer animationForKey:key];
if (animation) {
animations[key] = animation;
}
}
if (animations.count > 0) {
self.persistedAnimations = animations;
[layer MB_pauseLayer];
}
}
- (void)restoreLayerAnimationsAndResume {
CALayer *layer = self.layer;
if (!layer) {
return;
}
[self.persistedAnimations enumerateKeysAndObjectsUsingBlock:^(NSString *key, CAAnimation *animation, BOOL *stop) {
[layer addAnimation:animation forKey:key];
}];
if (self.persistedAnimations.count > 0) {
[layer MB_resumeLayer];
}
self.persistedAnimations = nil;
}
#pragma mark - Notifications
- (void)registerForAppStateNotifications {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationDidEnterBackground) name:UIApplicationDidEnterBackgroundNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillEnterForeground) name:UIApplicationWillEnterForegroundNotification object:nil];
}
- (void)unregisterFromAppStateNotifications {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)applicationDidEnterBackground {
[self persistLayerAnimationsAndPause];
}
- (void)applicationWillEnterForeground {
[self restoreLayerAnimationsAndResume];
}
@end
Copyright (c) 2014 Matej Bukovinski
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
@koedal
Copy link

koedal commented Mar 31, 2014

What software license are you using for this code?

@matej
Copy link
Author

matej commented Apr 1, 2014

Added a MIT license. I hope that works for you.

@koedal
Copy link

koedal commented Apr 1, 2014

Thanks!

@gastonmorixe
Copy link

WOOHA! Finally. Finally. Thank you!

@gastonmorixe
Copy link

@matej I think I found a bug. Competition block is not being called after the resumed ends, but if fires as soon as the app has resumed, not taking care of the real time left. Any idea if this could be solved? Could we know how much time left the animation has before going BG and at resume time we fire completion block with this saved duration? Thanks!

@matej
Copy link
Author

matej commented Oct 10, 2014

The completion block should be called when you enter background (which cancels the animation). At least that's what I remember seeing. It's not a problem for me as I don't use the completion block and have an infinitely repeating animation (UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat).

Check the layer timing properties (e.g., timeOffset), perhaps there's something useful for you there.

@nspassov
Copy link

Worked great for me using XCode 6.1

@ronherrema
Copy link

I'm getting an error message "Use of undeclared identifier 'UIApplicationWillEnterForegroundNotification'" in your .m file. I'm using Xcode 6.1.

@ronherrema
Copy link

OK, I added '#import <UIKit/UIKit.h>' to your.h file and that solved the problem. However, I'm facing a similar problem to imton. The time the app is in the background creates a delay of that length when I trigger the animation again.

@warpling
Copy link

@ronherrema I don't think this is the proper fix, but adding #import <UIKit/UIKit.h> to the header worked for me. What did you import?

@ArtFeel
Copy link

ArtFeel commented Apr 28, 2018

Excellent solution, I rewrite this to Swift 4

@warpling
Copy link

Thank you!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment