This repository has been archived by the owner on Aug 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for removing added animations (#42)
* Add an animation registrar. * Docs. * Misc consolidation. * Remove newline. * Comment. * Remove unused method. * Rename to stop. * Docs. * Generics. * Comments. * Runloop.
- Loading branch information
Showing
8 changed files
with
330 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
Copyright 2017-present The Material Motion Authors. All Rights Reserved. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
#import <Foundation/Foundation.h> | ||
#import <QuartzCore/QuartzCore.h> | ||
|
||
// Tracks and manipulates animations that have been added to a layer. | ||
@interface MDMAnimationRegistrar : NSObject | ||
|
||
// Invokes the layer's addAnimation:forKey: method with the provided animation and key and tracks | ||
// its association. Upon completion of the animation, the provided optional completion block will be | ||
// executed. | ||
- (void)addAnimation:(nonnull CABasicAnimation *)animation | ||
toLayer:(nonnull CALayer *)layer | ||
forKey:(nonnull NSString *)key | ||
completion:(void(^ __nullable)(void))completion; | ||
|
||
// For every active animation, reads the associated layer's presentation layer key path and writes | ||
// it to the layer. | ||
- (void)commitCurrentAnimationValuesToAllLayers; | ||
|
||
// Removes all active animations from their associated layer. | ||
- (void)removeAllAnimations; | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/* | ||
Copyright 2017-present The Material Motion Authors. All Rights Reserved. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
#import "MDMAnimationRegistrar.h" | ||
|
||
#import "MDMRegisteredAnimation.h" | ||
|
||
@implementation MDMAnimationRegistrar { | ||
NSMapTable<CALayer *, NSMutableSet<MDMRegisteredAnimation *> *> *_layersToRegisteredAnimation; | ||
} | ||
|
||
- (instancetype)init { | ||
self = [super init]; | ||
if (self) { | ||
_layersToRegisteredAnimation = [NSMapTable mapTableWithKeyOptions:NSPointerFunctionsWeakMemory | ||
valueOptions:NSPointerFunctionsStrongMemory]; | ||
} | ||
return self; | ||
} | ||
|
||
#pragma mark - Private | ||
|
||
- (void)forEachAnimation:(void (^)(CALayer *, CABasicAnimation *, NSString *))work { | ||
// Copy the registered animations before iteration in case further modifications happen to the | ||
// registered animations. Consider if we remove an animation, its associated completion block | ||
// might invoke logic that adds a new animation, potentially modifying our collections. | ||
for (CALayer *layer in [_layersToRegisteredAnimation copy]) { | ||
NSSet *keyPathAnimations = [_layersToRegisteredAnimation objectForKey:layer]; | ||
for (MDMRegisteredAnimation *keyPathAnimation in [keyPathAnimations copy]) { | ||
if (![keyPathAnimation.animation isKindOfClass:[CABasicAnimation class]]) { | ||
continue; | ||
} | ||
|
||
work(layer, [keyPathAnimation.animation copy], keyPathAnimation.key); | ||
} | ||
} | ||
} | ||
|
||
#pragma mark - Public | ||
|
||
- (void)addAnimation:(CABasicAnimation *)animation | ||
toLayer:(CALayer *)layer | ||
forKey:(NSString *)key | ||
completion:(void(^)(void))completion { | ||
if (key == nil) { | ||
key = [NSUUID UUID].UUIDString; | ||
} | ||
|
||
NSMutableSet *animatedKeyPaths = [_layersToRegisteredAnimation objectForKey:layer]; | ||
if (!animatedKeyPaths) { | ||
animatedKeyPaths = [[NSMutableSet alloc] init]; | ||
[_layersToRegisteredAnimation setObject:animatedKeyPaths forKey:layer]; | ||
} | ||
MDMRegisteredAnimation *keyPathAnimation = | ||
[[MDMRegisteredAnimation alloc] initWithKey:key animation:animation]; | ||
[animatedKeyPaths addObject:keyPathAnimation]; | ||
|
||
[CATransaction begin]; | ||
[CATransaction setCompletionBlock:^{ | ||
[animatedKeyPaths removeObject:keyPathAnimation]; | ||
|
||
if (completion) { | ||
completion(); | ||
} | ||
}]; | ||
|
||
[layer addAnimation:animation forKey:key]; | ||
|
||
[CATransaction commit]; | ||
} | ||
|
||
- (void)commitCurrentAnimationValuesToAllLayers { | ||
[self forEachAnimation:^(CALayer *layer, CABasicAnimation *animation, NSString *key) { | ||
id presentationLayer = [layer presentationLayer]; | ||
if (presentationLayer != nil) { | ||
id presentationValue = [presentationLayer valueForKeyPath:animation.keyPath]; | ||
[layer setValue:presentationValue forKeyPath:animation.keyPath]; | ||
} | ||
}]; | ||
} | ||
|
||
- (void)removeAllAnimations { | ||
[self forEachAnimation:^(CALayer *layer, CABasicAnimation *animation, NSString *key) { | ||
[layer removeAnimationForKey:key]; | ||
}]; | ||
[_layersToRegisteredAnimation removeAllObjects]; | ||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
Copyright 2017-present The Material Motion Authors. All Rights Reserved. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
#import <Foundation/Foundation.h> | ||
#import <QuartzCore/QuartzCore.h> | ||
|
||
@interface MDMRegisteredAnimation : NSObject | ||
|
||
- (instancetype)initWithKey:(NSString *)key animation:(CABasicAnimation *)animation; | ||
|
||
@property(nonatomic, copy, readonly) NSString *key; | ||
|
||
@property(nonatomic, strong, readonly) CABasicAnimation *animation; | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
Copyright 2017-present The Material Motion Authors. All Rights Reserved. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
#import "MDMRegisteredAnimation.h" | ||
|
||
@implementation MDMRegisteredAnimation | ||
|
||
- (instancetype)initWithKey:(NSString *)key animation:(CABasicAnimation *)animation { | ||
self = [super init]; | ||
if (self) { | ||
_key = [key copy]; | ||
_animation = animation; | ||
} | ||
return self; | ||
} | ||
|
||
- (NSUInteger)hash { | ||
return _key.hash; | ||
} | ||
|
||
- (BOOL)isEqual:(id)object { | ||
return [_key isEqual:object]; | ||
} | ||
|
||
@end | ||
|
Oops, something went wrong.