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
Add support for removing added animations #42
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
bf217df
Add an animation registrar.
c8dd35a
Docs.
53303ff
Misc consolidation.
cf76ff8
Remove newline.
36eee48
Comment.
a202eba
Remove unused method.
a65a3bc
Rename to stop.
9c97c17
Docs.
8b97dfc
Generics.
b303a04
Comments.
91466fe
Runloop.
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Two registered animations are equal if they have the same key but different animations? Could you please add some basic unit tests for this class? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't have a convention in place for private API unit tests unfortunately, meaning we can't access the APIs from unit test targets without some change to both our podfile & bazel configs. |
||
} | ||
|
||
@end | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking at this comment, are you worried that the dictionary can be mutated while you are iterating over it? Are the
work
blocks the source of mutation?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep - consider if we remove an animation, its associated completion block might get fired and a new animation might be registered as a result.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Got it, thanks!