Skip to content
This repository has been archived by the owner on Aug 30, 2023. It is now read-only.

Add support for removing added animations #42

Merged
merged 11 commits into from
Nov 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
667A3F4C1DEE269400CB3A99 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 667A3F4A1DEE269400CB3A99 /* LaunchScreen.storyboard */; };
667A3F541DEE273000CB3A99 /* TableOfContents.swift in Sources */ = {isa = PBXBuildFile; fileRef = 667A3F531DEE273000CB3A99 /* TableOfContents.swift */; };
6687264A1EF04B4C00113675 /* MotionAnimatorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 668726491EF04B4C00113675 /* MotionAnimatorTests.swift */; };
66A6A6681FBA158000DE54CB /* AnimationRemovalTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 66A6A6671FBA158000DE54CB /* AnimationRemovalTests.swift */; };
66BF5A8F1FB0E4CB00E864F6 /* ImplicitAnimationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 66BF5A8E1FB0E4CB00E864F6 /* ImplicitAnimationTests.swift */; };
66DD4BF51EEF0ECB00207119 /* CalendarCardExpansionExample.m in Sources */ = {isa = PBXBuildFile; fileRef = 66DD4BF41EEF0ECB00207119 /* CalendarCardExpansionExample.m */; };
66DD4BF81EEF1C4B00207119 /* CalendarChipMotionSpec.m in Sources */ = {isa = PBXBuildFile; fileRef = 66DD4BF71EEF1C4B00207119 /* CalendarChipMotionSpec.m */; };
Expand Down Expand Up @@ -62,6 +63,7 @@
667A3F4D1DEE269400CB3A99 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
667A3F531DEE273000CB3A99 /* TableOfContents.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TableOfContents.swift; sourceTree = "<group>"; };
668726491EF04B4C00113675 /* MotionAnimatorTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MotionAnimatorTests.swift; sourceTree = "<group>"; };
66A6A6671FBA158000DE54CB /* AnimationRemovalTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AnimationRemovalTests.swift; sourceTree = "<group>"; };
66BF5A8E1FB0E4CB00E864F6 /* ImplicitAnimationTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ImplicitAnimationTests.swift; sourceTree = "<group>"; };
66DD4BF31EEF0ECB00207119 /* CalendarCardExpansionExample.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = CalendarCardExpansionExample.h; sourceTree = "<group>"; };
66DD4BF41EEF0ECB00207119 /* CalendarCardExpansionExample.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = CalendarCardExpansionExample.m; sourceTree = "<group>"; };
Expand Down Expand Up @@ -208,6 +210,7 @@
66FD99F81EE9FBA000C53A82 /* unit */ = {
isa = PBXGroup;
children = (
66A6A6671FBA158000DE54CB /* AnimationRemovalTests.swift */,
66FD99F91EE9FBBE00C53A82 /* MotionAnimatorTests.m */,
668726491EF04B4C00113675 /* MotionAnimatorTests.swift */,
66BF5A8E1FB0E4CB00E864F6 /* ImplicitAnimationTests.swift */,
Expand Down Expand Up @@ -491,6 +494,7 @@
6625876C1FB4DB9C00BC7DF1 /* InitialVelocityTests.swift in Sources */,
660636021FACC24300C3DFB8 /* TimeScaleFactorTests.swift in Sources */,
66BF5A8F1FB0E4CB00E864F6 /* ImplicitAnimationTests.swift in Sources */,
66A6A6681FBA158000DE54CB /* AnimationRemovalTests.swift in Sources */,
6687264A1EF04B4C00113675 /* MotionAnimatorTests.swift in Sources */,
66FD99FA1EE9FBBE00C53A82 /* MotionAnimatorTests.m in Sources */,
);
Expand Down
17 changes: 17 additions & 0 deletions src/MDMMotionAnimator.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,4 +132,21 @@ NS_SWIFT_NAME(MotionAnimator)
animations:(nonnull void (^)(void))animations
completion:(nullable void(^)(void))completion;

/**
Removes every animation added by this animator.

Removing animations in this manner will give the appearance of each animated layer property
instantaneously jumping to its animated destination.
*/
- (void)removeAllAnimations;

/**
Commits the presentation layer value to the model layer value for every active animation's key path
and then removes every animation.

This method is most commonly called in reaction to the initiation of a gesture so that any
in-flight animations are stopped at their current on-screen position.
*/
- (void)stopAllAnimations;

@end
26 changes: 13 additions & 13 deletions src/MDMMotionAnimator.m
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,20 @@

#import "CATransaction+MotionAnimator.h"
#import "private/CABasicAnimation+MotionAnimator.h"
#import "private/MDMAnimationRegistrar.h"
#import "private/MDMUIKitValueCoercion.h"
#import "private/MDMBlockAnimations.h"
#import "private/MDMDragCoefficient.h"

@implementation MDMMotionAnimator {
NSMutableArray *_tracers;
MDMAnimationRegistrar *_registrar;
}

- (instancetype)init {
self = [super init];
if (self) {
_registrar = [[MDMAnimationRegistrar alloc] init];
_timeScaleFactor = 1;
_additive = true;
}
Expand Down Expand Up @@ -93,23 +96,12 @@ - (void)animateWithTiming:(MDMMotionTiming)timing
animation.fillMode = kCAFillModeBackwards;
}

if (completion) {
[CATransaction begin];
[CATransaction setCompletionBlock:completion];
}

// When we use a nil key, Core Animation will ensure that the animation is added with a
// unique key - this enables our additive animations to stack upon one another.
NSString *key = _additive ? nil : keyPath;
[layer addAnimation:animation forKey:key];
[_registrar addAnimation:animation toLayer:layer forKey:key completion:completion];

for (void (^tracer)(CALayer *, CAAnimation *) in _tracers) {
tracer(layer, animation);
}

if (completion) {
[CATransaction commit];
}
}
}

Expand Down Expand Up @@ -165,5 +157,13 @@ - (CGFloat)computedTimeScaleFactor {
return MDMSimulatorAnimationDragCoefficient() * timeScaleFactor;
}

@end
- (void)removeAllAnimations {
[_registrar removeAllAnimations];
}

- (void)stopAllAnimations {
[_registrar commitCurrentAnimationValuesToAllLayers];
[_registrar removeAllAnimations];
}

@end
38 changes: 38 additions & 0 deletions src/private/MDMAnimationRegistrar.h
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
102 changes: 102 additions & 0 deletions src/private/MDMAnimationRegistrar.m
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
Copy link
Contributor

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?

Copy link
Contributor Author

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.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it, thanks!

// 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
28 changes: 28 additions & 0 deletions src/private/MDMRegisteredAnimation.h
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
39 changes: 39 additions & 0 deletions src/private/MDMRegisteredAnimation.m
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];
Copy link
Contributor

Choose a reason for hiding this comment

The 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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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

Loading