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 new APIs for implicit animations. #30
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
d08bd4d
Add new APIs for implicit animations.
299185d
Docs.
58ba8bb
Add docs.
a65e0fb
Docs.
cb4ec6a
Cleanup.
82cadd9
Remove strong.
0a66afc
Return copy.
2dc032a
Docs and rework.
5de1198
Add missing header.
e4bb1ba
More tests.
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,26 @@ | ||
/* | ||
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 MDMImplicitAction: NSObject | ||
@property(nonatomic, strong, readonly) id initialValue; | ||
@property(nonatomic, copy, readonly) NSString *keyPath; | ||
@property(nonatomic, strong, readonly) CALayer *layer; | ||
@end | ||
|
||
NSArray<MDMImplicitAction *> *MDMAnimateImplicitly(void (^animations)(void)); |
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,132 @@ | ||
/* | ||
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 "MDMBlockAnimations.h" | ||
|
||
#import <UIKit/UIKit.h> | ||
#import <objc/runtime.h> | ||
|
||
static IMP sOriginalActionForLayerImp = NULL; | ||
|
||
@interface MDMActionContext: NSObject | ||
@property(nonatomic, readonly) NSArray<MDMImplicitAction *> *interceptedActions; | ||
@end | ||
|
||
@implementation MDMImplicitAction | ||
|
||
- (instancetype)initWithLayer:(CALayer *)layer | ||
keyPath:(NSString *)keyPath | ||
initialValue:(id)initialValue { | ||
self = [super init]; | ||
if (self) { | ||
_layer = layer; | ||
_keyPath = [keyPath copy]; | ||
_initialValue = initialValue; | ||
} | ||
return self; | ||
} | ||
|
||
@end | ||
|
||
@implementation MDMActionContext { | ||
NSMutableArray<MDMImplicitAction *> *_interceptedActions; | ||
} | ||
|
||
- (instancetype)init { | ||
self = [super init]; | ||
if (self) { | ||
_interceptedActions = [NSMutableArray array]; | ||
} | ||
return self; | ||
} | ||
|
||
- (void)addActionForLayer:(CALayer *)layer | ||
keyPath:(NSString *)keyPath | ||
withInitialValue:(id)initialValue { | ||
[_interceptedActions addObject:[[MDMImplicitAction alloc] initWithLayer:layer | ||
keyPath:keyPath | ||
initialValue:initialValue]]; | ||
} | ||
|
||
- (NSArray<MDMImplicitAction *> *)interceptedActions { | ||
return [_interceptedActions copy]; | ||
} | ||
|
||
@end | ||
|
||
static NSMutableArray *sActionContext = nil; | ||
|
||
static id<CAAction> ActionForLayer(id self, SEL _cmd, CALayer *layer, NSString *event) { | ||
NSCAssert([NSStringFromSelector(_cmd) isEqualToString: | ||
NSStringFromSelector(@selector(actionForLayer:forKey:))], | ||
@"Invalid method signature."); | ||
|
||
MDMActionContext *context = [sActionContext lastObject]; | ||
NSCAssert(context != nil, @"MotionAnimator action method invoked out of implicit scope."); | ||
|
||
if (context == nil) { | ||
// Graceful handling of invalid state on non-debug builds for if our context is nil invokes our | ||
// original implementation: | ||
return ((id<CAAction>(*)(id, SEL, CALayer *, NSString *))sOriginalActionForLayerImp) | ||
(self, _cmd, layer, event); | ||
} | ||
|
||
// We don't have access to the "to" value of our animation here, so we unfortunately can't | ||
// calculate additive values if the animator is configured as such. So, to support additive | ||
// animations, we queue up the modified actions and then add them all at the end of our | ||
// MDMAnimateBlock invocation. | ||
id initialValue = [layer valueForKeyPath:event]; | ||
[context addActionForLayer:layer keyPath:event withInitialValue:initialValue]; | ||
return [NSNull null]; | ||
} | ||
|
||
NSArray<MDMImplicitAction *> *MDMAnimateImplicitly(void (^work)(void)) { | ||
if (!work) { | ||
return nil; | ||
} | ||
|
||
// This method can be called recursively, so we maintain a recursive context stack in the scope of | ||
// this method. Note that this is absolutely not thread safe, but neither is Core Animation. | ||
if (!sActionContext) { | ||
sActionContext = [NSMutableArray array]; | ||
} | ||
[sActionContext addObject:[[MDMActionContext alloc] init]]; | ||
|
||
SEL selector = @selector(actionForLayer:forKey:); | ||
Method method = class_getInstanceMethod([UIView class], selector); | ||
|
||
if (sOriginalActionForLayerImp == nil) { | ||
// Swap the original UIView implementation with our own so that we can intercept all | ||
// actionForLayer:forKey: events. All events will be | ||
sOriginalActionForLayerImp = method_setImplementation(method, (IMP)ActionForLayer); | ||
} | ||
|
||
work(); | ||
|
||
// Return any intercepted actions we received during the invocation of work. | ||
MDMActionContext *context = [sActionContext lastObject]; | ||
[sActionContext removeLastObject]; | ||
|
||
if ([sActionContext count] == 0) { | ||
// Restore our original method if we've emptied the stack: | ||
method_setImplementation(method, sOriginalActionForLayerImp); | ||
|
||
sOriginalActionForLayerImp = nil; | ||
sActionContext = nil; | ||
} | ||
|
||
return context.interceptedActions; | ||
} |
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.
Can we document the retain semantics of the block for callers? Is it as safe (unretained) as UIKit animations?
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.
Done.