|
| 1 | +/* |
| 2 | + Copyright 2017-present The Material Motion Authors. All Rights Reserved. |
| 3 | +
|
| 4 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + you may not use this file except in compliance with the License. |
| 6 | + You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | + Unless required by applicable law or agreed to in writing, software |
| 11 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + See the License for the specific language governing permissions and |
| 14 | + limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +#import "MDMBlockAnimations.h" |
| 18 | + |
| 19 | +#import <UIKit/UIKit.h> |
| 20 | +#import <objc/runtime.h> |
| 21 | + |
| 22 | +static IMP sOriginalActionForLayerImp = NULL; |
| 23 | + |
| 24 | +@interface MDMActionContext: NSObject |
| 25 | +@property(nonatomic, readonly) NSArray<MDMImplicitAction *> *interceptedActions; |
| 26 | +@end |
| 27 | + |
| 28 | +@implementation MDMImplicitAction |
| 29 | + |
| 30 | +- (instancetype)initWithLayer:(CALayer *)layer |
| 31 | + keyPath:(NSString *)keyPath |
| 32 | + initialValue:(id)initialValue { |
| 33 | + self = [super init]; |
| 34 | + if (self) { |
| 35 | + _layer = layer; |
| 36 | + _keyPath = [keyPath copy]; |
| 37 | + _initialValue = initialValue; |
| 38 | + } |
| 39 | + return self; |
| 40 | +} |
| 41 | + |
| 42 | +@end |
| 43 | + |
| 44 | +@implementation MDMActionContext { |
| 45 | + NSMutableArray<MDMImplicitAction *> *_interceptedActions; |
| 46 | +} |
| 47 | + |
| 48 | +- (instancetype)init { |
| 49 | + self = [super init]; |
| 50 | + if (self) { |
| 51 | + _interceptedActions = [NSMutableArray array]; |
| 52 | + } |
| 53 | + return self; |
| 54 | +} |
| 55 | + |
| 56 | +- (void)addActionForLayer:(CALayer *)layer |
| 57 | + keyPath:(NSString *)keyPath |
| 58 | + withInitialValue:(id)initialValue { |
| 59 | + [_interceptedActions addObject:[[MDMImplicitAction alloc] initWithLayer:layer |
| 60 | + keyPath:keyPath |
| 61 | + initialValue:initialValue]]; |
| 62 | +} |
| 63 | + |
| 64 | +- (NSArray<MDMImplicitAction *> *)interceptedActions { |
| 65 | + return [_interceptedActions copy]; |
| 66 | +} |
| 67 | + |
| 68 | +@end |
| 69 | + |
| 70 | +static NSMutableArray *sActionContext = nil; |
| 71 | + |
| 72 | +static id<CAAction> ActionForLayer(id self, SEL _cmd, CALayer *layer, NSString *event) { |
| 73 | + NSCAssert([NSStringFromSelector(_cmd) isEqualToString: |
| 74 | + NSStringFromSelector(@selector(actionForLayer:forKey:))], |
| 75 | + @"Invalid method signature."); |
| 76 | + |
| 77 | + MDMActionContext *context = [sActionContext lastObject]; |
| 78 | + NSCAssert(context != nil, @"MotionAnimator action method invoked out of implicit scope."); |
| 79 | + |
| 80 | + if (context == nil) { |
| 81 | + // Graceful handling of invalid state on non-debug builds for if our context is nil invokes our |
| 82 | + // original implementation: |
| 83 | + return ((id<CAAction>(*)(id, SEL, CALayer *, NSString *))sOriginalActionForLayerImp) |
| 84 | + (self, _cmd, layer, event); |
| 85 | + } |
| 86 | + |
| 87 | + // We don't have access to the "to" value of our animation here, so we unfortunately can't |
| 88 | + // calculate additive values if the animator is configured as such. So, to support additive |
| 89 | + // animations, we queue up the modified actions and then add them all at the end of our |
| 90 | + // MDMAnimateBlock invocation. |
| 91 | + id initialValue = [layer valueForKeyPath:event]; |
| 92 | + [context addActionForLayer:layer keyPath:event withInitialValue:initialValue]; |
| 93 | + return [NSNull null]; |
| 94 | +} |
| 95 | + |
| 96 | +NSArray<MDMImplicitAction *> *MDMAnimateImplicitly(void (^work)(void)) { |
| 97 | + if (!work) { |
| 98 | + return nil; |
| 99 | + } |
| 100 | + |
| 101 | + // This method can be called recursively, so we maintain a recursive context stack in the scope of |
| 102 | + // this method. Note that this is absolutely not thread safe, but neither is Core Animation. |
| 103 | + if (!sActionContext) { |
| 104 | + sActionContext = [NSMutableArray array]; |
| 105 | + } |
| 106 | + [sActionContext addObject:[[MDMActionContext alloc] init]]; |
| 107 | + |
| 108 | + SEL selector = @selector(actionForLayer:forKey:); |
| 109 | + Method method = class_getInstanceMethod([UIView class], selector); |
| 110 | + |
| 111 | + if (sOriginalActionForLayerImp == nil) { |
| 112 | + // Swap the original UIView implementation with our own so that we can intercept all |
| 113 | + // actionForLayer:forKey: events. All events will be |
| 114 | + sOriginalActionForLayerImp = method_setImplementation(method, (IMP)ActionForLayer); |
| 115 | + } |
| 116 | + |
| 117 | + work(); |
| 118 | + |
| 119 | + // Return any intercepted actions we received during the invocation of work. |
| 120 | + MDMActionContext *context = [sActionContext lastObject]; |
| 121 | + [sActionContext removeLastObject]; |
| 122 | + |
| 123 | + if ([sActionContext count] == 0) { |
| 124 | + // Restore our original method if we've emptied the stack: |
| 125 | + method_setImplementation(method, sOriginalActionForLayerImp); |
| 126 | + |
| 127 | + sOriginalActionForLayerImp = nil; |
| 128 | + sActionContext = nil; |
| 129 | + } |
| 130 | + |
| 131 | + return context.interceptedActions; |
| 132 | +} |
0 commit comments