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

Commit f743ca0

Browse files
authored
Add MDMAllAnimatableKeyPaths API for retrieving all animatable key paths. (#69)
1 parent b0a5f37 commit f743ca0

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

src/private/MDMBlockAnimations.m

+28-3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,31 @@
2121
#import <UIKit/UIKit.h>
2222
#import <objc/runtime.h>
2323

24+
// Returns the set of animatable key paths supported by MDMMotionAnimator's implicit animations.
25+
static NSSet<MDMAnimatableKeyPath> *AllAnimatableKeyPaths(void) {
26+
static NSSet *animatableKeyPaths = nil;
27+
static dispatch_once_t onceToken;
28+
dispatch_once(&onceToken, ^{
29+
animatableKeyPaths = [NSSet setWithArray:@[MDMKeyPathBackgroundColor,
30+
MDMKeyPathBounds,
31+
MDMKeyPathCornerRadius,
32+
MDMKeyPathHeight,
33+
MDMKeyPathOpacity,
34+
MDMKeyPathPosition,
35+
MDMKeyPathRotation,
36+
MDMKeyPathScale,
37+
MDMKeyPathShadowOffset,
38+
MDMKeyPathShadowOpacity,
39+
MDMKeyPathShadowRadius,
40+
MDMKeyPathStrokeStart,
41+
MDMKeyPathStrokeEnd,
42+
MDMKeyPathWidth,
43+
MDMKeyPathX,
44+
MDMKeyPathY]];
45+
});
46+
return animatableKeyPaths;
47+
}
48+
2449
@interface MDMActionContext: NSObject
2550
@property(nonatomic, readonly) NSArray<MDMImplicitAction *> *interceptedActions;
2651
@end
@@ -83,9 +108,9 @@ @interface MDMLayerDelegate: NSObject <CALayerDelegate>
83108
MDMActionContext *context = [sActionContext lastObject];
84109
NSCAssert(context != nil, @"MotionAnimator action method invoked out of implicit scope.");
85110

86-
if (context == nil) {
87-
// Graceful handling of invalid state on non-debug builds for if our context is nil invokes our
88-
// original implementation:
111+
BOOL shouldAnimateWithAnimator = [AllAnimatableKeyPaths() containsObject:event];
112+
if (context == nil || !shouldAnimateWithAnimator) {
113+
// Fall through to the original CALayer implementation.
89114
return ((id<CAAction>(*)(id, SEL, NSString *))sOriginalActionForKeyLayerImp)
90115
(layer, _cmd, event);
91116
}

tests/unit/ImplicitAnimationTests.swift

+10
Original file line numberDiff line numberDiff line change
@@ -209,4 +209,14 @@ class ImplicitAnimationTests: XCTestCase {
209209

210210
XCTAssertEqual(view.layer.animationKeys()!, ["opacity"])
211211
}
212+
213+
func testUnsupportedAnimationKeyIsNotAnimated() {
214+
animator.animate(with: timing) {
215+
self.view.layer.sublayers = []
216+
}
217+
218+
XCTAssertNil(view.layer.animationKeys(),
219+
"No animations should have been added, but the following keys were found: "
220+
+ "\(view.layer.animationKeys()!)")
221+
}
212222
}

0 commit comments

Comments
 (0)