-
Notifications
You must be signed in to change notification settings - Fork 25.1k
Add support for delete animation in LayoutAnimation on iOS #6779
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
6c37d2f
Support delete animation for LayoutAnimation on iOS
janicduplessis 35533a8
Clear the layout animation properly at the end of the layout
janicduplessis 99df8bd
Add crossfade example
janicduplessis 5f41e50
Fix nits and add userInteractionEnabled = NO during delete animation
janicduplessis 7b07bcb
Fix React import
janicduplessis 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 hidden or 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,171 @@ | ||
| /** | ||
| * The examples provided by Facebook are for non-commercial testing and | ||
| * evaluation purposes only. | ||
| * | ||
| * Facebook reserves all rights not expressly granted. | ||
| * | ||
| * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS | ||
| * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| * FITNESS FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL | ||
| * FACEBOOK BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN | ||
| * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||
| * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
| * | ||
| * @flow | ||
| */ | ||
| 'use strict'; | ||
|
|
||
| const React = require('react'); | ||
| const ReactNative = require('react-native'); | ||
| const { | ||
| LayoutAnimation, | ||
| StyleSheet, | ||
| Text, | ||
| View, | ||
| TouchableOpacity, | ||
| } = ReactNative; | ||
|
|
||
| const AddRemoveExample = React.createClass({ | ||
|
|
||
| getInitialState() { | ||
| return { | ||
| views: [], | ||
| }; | ||
| }, | ||
|
|
||
| componentWillUpdate() { | ||
| LayoutAnimation.easeInEaseOut(); | ||
| }, | ||
|
|
||
| _onPressAddView() { | ||
| this.setState((state) => ({views: [...state.views, {}]})); | ||
| }, | ||
|
|
||
| _onPressRemoveView() { | ||
| this.setState((state) => ({views: state.views.slice(0, -1)})); | ||
| }, | ||
|
|
||
| render() { | ||
| const views = this.state.views.map((view, i) => | ||
| <View key={i} style={styles.view}> | ||
| <Text>{i}</Text> | ||
| </View> | ||
| ); | ||
| return ( | ||
| <View style={styles.container}> | ||
| <TouchableOpacity onPress={this._onPressAddView}> | ||
| <View style={styles.button}> | ||
| <Text>Add view</Text> | ||
| </View> | ||
| </TouchableOpacity> | ||
| <TouchableOpacity onPress={this._onPressRemoveView}> | ||
| <View style={styles.button}> | ||
| <Text>Remove view</Text> | ||
| </View> | ||
| </TouchableOpacity> | ||
| <View style={styles.viewContainer}> | ||
| {views} | ||
| </View> | ||
| </View> | ||
| ); | ||
| }, | ||
| }); | ||
|
|
||
| const GreenSquare = () => | ||
| <View style={styles.greenSquare}> | ||
| <Text>Green square</Text> | ||
| </View>; | ||
|
|
||
| const BlueSquare = () => | ||
| <View style={styles.blueSquare}> | ||
| <Text>Blue square</Text> | ||
| </View>; | ||
|
|
||
| const CrossFadeExample = React.createClass({ | ||
|
|
||
| getInitialState() { | ||
| return { | ||
| toggled: false, | ||
| }; | ||
| }, | ||
|
|
||
| _onPressToggle() { | ||
| LayoutAnimation.easeInEaseOut(); | ||
| this.setState((state) => ({toggled: !state.toggled})); | ||
| }, | ||
|
|
||
| render() { | ||
| return ( | ||
| <View style={styles.container}> | ||
| <TouchableOpacity onPress={this._onPressToggle}> | ||
| <View style={styles.button}> | ||
| <Text>Toggle</Text> | ||
| </View> | ||
| </TouchableOpacity> | ||
| <View style={styles.viewContainer}> | ||
| { | ||
| this.state.toggled ? | ||
| <GreenSquare /> : | ||
| <BlueSquare /> | ||
| } | ||
| </View> | ||
| </View> | ||
| ); | ||
| }, | ||
| }); | ||
|
|
||
| const styles = StyleSheet.create({ | ||
| container: { | ||
| flex: 1, | ||
| }, | ||
| button: { | ||
| borderRadius: 5, | ||
| backgroundColor: '#eeeeee', | ||
| padding: 10, | ||
| marginBottom: 10, | ||
| }, | ||
| buttonText: { | ||
| fontSize: 16, | ||
| }, | ||
| viewContainer: { | ||
| flex: 1, | ||
| flexDirection: 'row', | ||
| flexWrap: 'wrap', | ||
| }, | ||
| view: { | ||
| height: 54, | ||
| width: 54, | ||
| backgroundColor: 'red', | ||
| margin: 8, | ||
| alignItems: 'center', | ||
| justifyContent: 'center', | ||
| }, | ||
| greenSquare: { | ||
| width: 150, | ||
| height: 150, | ||
| backgroundColor: 'green', | ||
| alignItems: 'center', | ||
| justifyContent: 'center', | ||
| }, | ||
| blueSquare: { | ||
| width: 150, | ||
| height: 150, | ||
| backgroundColor: 'blue', | ||
| alignItems: 'center', | ||
| justifyContent: 'center', | ||
| }, | ||
| }); | ||
|
|
||
| exports.title = 'Layout Animation'; | ||
| exports.description = 'Layout animation'; | ||
| exports.examples = [{ | ||
| title: 'Add and remove views', | ||
| render(): ReactElement { | ||
| return <AddRemoveExample />; | ||
| }, | ||
| }, { | ||
| title: 'Cross fade views', | ||
| render(): ReactElement { | ||
| return <CrossFadeExample />; | ||
| }, | ||
| }]; | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -191,7 +191,6 @@ @implementation RCTUIManager | |
| NSMutableArray<dispatch_block_t> *_pendingUIBlocks; | ||
|
|
||
| // Animation | ||
| RCTLayoutAnimation *_nextLayoutAnimation; // RCT thread only | ||
| RCTLayoutAnimation *_layoutAnimation; // Main thread only | ||
|
|
||
| NSMutableDictionary<NSNumber *, RCTShadowView *> *_shadowViewRegistry; // RCT thread only | ||
|
|
@@ -582,11 +581,7 @@ - (RCTViewManagerUIBlock)uiBlockWithLayoutUpdateForRootView:(RCTRootShadowView * | |
|
|
||
| // Perform layout (possibly animated) | ||
| return ^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, UIView *> *viewRegistry) { | ||
| RCTResponseSenderBlock callback = self->_layoutAnimation.callback; | ||
|
|
||
| // It's unsafe to call this callback more than once, so we nil it out here | ||
| // to make sure that doesn't happen. | ||
| _layoutAnimation.callback = nil; | ||
| RCTLayoutAnimation *layoutAnimation = _layoutAnimation; | ||
|
|
||
| __block NSUInteger completionsCalled = 0; | ||
| for (NSUInteger ii = 0; ii < frames.count; ii++) { | ||
|
|
@@ -595,14 +590,18 @@ - (RCTViewManagerUIBlock)uiBlockWithLayoutUpdateForRootView:(RCTRootShadowView * | |
| CGRect frame = [frames[ii] CGRectValue]; | ||
|
|
||
| BOOL isNew = [areNew[ii] boolValue]; | ||
| RCTAnimation *updateAnimation = isNew ? nil : _layoutAnimation.updateAnimation; | ||
| RCTAnimation *updateAnimation = isNew ? nil : layoutAnimation.updateAnimation; | ||
| BOOL shouldAnimateCreation = isNew && ![parentsAreNew[ii] boolValue]; | ||
| RCTAnimation *createAnimation = shouldAnimateCreation ? _layoutAnimation.createAnimation : nil; | ||
| RCTAnimation *createAnimation = shouldAnimateCreation ? layoutAnimation.createAnimation : nil; | ||
|
|
||
| void (^completion)(BOOL) = ^(BOOL finished) { | ||
| completionsCalled++; | ||
| if (callback && completionsCalled == frames.count) { | ||
| callback(@[@(finished)]); | ||
| if (layoutAnimation.callback && completionsCalled == frames.count) { | ||
| layoutAnimation.callback(@[@(finished)]); | ||
|
|
||
| // It's unsafe to call this callback more than once, so we nil it out here | ||
| // to make sure that doesn't happen. | ||
| layoutAnimation.callback = nil; | ||
| } | ||
| }; | ||
|
|
||
|
|
@@ -656,6 +655,8 @@ - (RCTViewManagerUIBlock)uiBlockWithLayoutUpdateForRootView:(RCTRootShadowView * | |
| completion(YES); | ||
| } | ||
| } | ||
|
|
||
| _layoutAnimation = nil; | ||
| }; | ||
| } | ||
|
|
||
|
|
@@ -729,9 +730,49 @@ - (void)_amendPendingUIBlocksWithStylePropagationUpdateForShadowView:(RCTShadowV | |
|
|
||
| - (void)_removeChildren:(NSArray<id<RCTComponent>> *)children | ||
| fromContainer:(id<RCTComponent>)container | ||
| permanent: (BOOL)permanent | ||
| { | ||
| RCTLayoutAnimation *layoutAnimation = _layoutAnimation; | ||
| RCTAnimation *deleteAnimation = layoutAnimation.deleteAnimation; | ||
|
|
||
| __block NSUInteger completionsCalled = 0; | ||
|
|
||
| for (id<RCTComponent> removedChild in children) { | ||
| [container removeReactSubview:removedChild]; | ||
|
|
||
| void (^completion)(BOOL) = ^(BOOL finished) { | ||
| completionsCalled++; | ||
|
|
||
| [container removeReactSubview:removedChild]; | ||
|
|
||
| if (layoutAnimation.callback && completionsCalled == children.count) { | ||
| layoutAnimation.callback(@[@(finished)]); | ||
|
|
||
| // It's unsafe to call this callback more than once, so we nil it out here | ||
| // to make sure that doesn't happen. | ||
| layoutAnimation.callback = nil; | ||
| } | ||
| }; | ||
|
|
||
| if (permanent && deleteAnimation && [removedChild isKindOfClass: [UIView class]]) { | ||
| UIView *view = (UIView *)removedChild; | ||
|
|
||
| // Disable user interaction while the view is animating since JS won't receive | ||
| // the view events anyway. | ||
| view.userInteractionEnabled = NO; | ||
|
|
||
| [deleteAnimation performAnimations:^{ | ||
| if ([deleteAnimation.property isEqual:@"scaleXY"]) { | ||
| view.layer.transform = CATransform3DMakeScale(0, 0, 0); | ||
| } else if ([deleteAnimation.property isEqual:@"opacity"]) { | ||
| view.layer.opacity = 0.0; | ||
| } else { | ||
| RCTLogError(@"Unsupported layout animation createConfig property %@", | ||
| deleteAnimation.property); | ||
| } | ||
| } withCompletionBlock:completion]; | ||
|
Contributor
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. I'm worried about weird edge-cases that could happen if the user interacts with the app while this deletion animation is running. Perhaps we should make the view immediately non-interactable? Any other safeguards we could use? How much did you bang on the prototype? |
||
| } else { | ||
| [container removeReactSubview:removedChild]; | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -848,8 +889,8 @@ - (void)_manageChildren:(NSNumber *)containerReactTag | |
| [self _childrenToRemoveFromContainer:container atIndices:removeAtIndices]; | ||
| NSArray<id<RCTComponent>> *temporarilyRemovedChildren = | ||
| [self _childrenToRemoveFromContainer:container atIndices:moveFromIndices]; | ||
| [self _removeChildren:permanentlyRemovedChildren fromContainer:container]; | ||
| [self _removeChildren:temporarilyRemovedChildren fromContainer:container]; | ||
| [self _removeChildren:permanentlyRemovedChildren fromContainer:container permanent:true]; | ||
| [self _removeChildren:temporarilyRemovedChildren fromContainer:container permanent:false]; | ||
|
|
||
| [self _purgeChildren:permanentlyRemovedChildren fromRegistry:registry]; | ||
|
|
||
|
|
@@ -1015,29 +1056,13 @@ - (void)_layoutAndMount | |
| [self addUIBlock:uiBlock]; | ||
| } | ||
|
|
||
| // Set up next layout animation | ||
| if (_nextLayoutAnimation) { | ||
| RCTLayoutAnimation *layoutAnimation = _nextLayoutAnimation; | ||
| [self addUIBlock:^(RCTUIManager *uiManager, __unused NSDictionary<NSNumber *, UIView *> *viewRegistry) { | ||
| uiManager->_layoutAnimation = layoutAnimation; | ||
| }]; | ||
| } | ||
|
|
||
| // Perform layout | ||
| for (NSNumber *reactTag in _rootViewTags) { | ||
| RCTRootShadowView *rootView = (RCTRootShadowView *)_shadowViewRegistry[reactTag]; | ||
| [self addUIBlock:[self uiBlockWithLayoutUpdateForRootView:rootView]]; | ||
| [self _amendPendingUIBlocksWithStylePropagationUpdateForShadowView:rootView]; | ||
| } | ||
|
|
||
| // Clear layout animations | ||
| if (_nextLayoutAnimation) { | ||
| [self addUIBlock:^(RCTUIManager *uiManager, __unused NSDictionary<NSNumber *, UIView *> *viewRegistry) { | ||
| uiManager->_layoutAnimation = nil; | ||
| }]; | ||
| _nextLayoutAnimation = nil; | ||
| } | ||
|
|
||
| [self addUIBlock:^(RCTUIManager *uiManager, __unused NSDictionary<NSNumber *, UIView *> *viewRegistry) { | ||
| /** | ||
| * TODO(tadeu): Remove it once and for all | ||
|
|
@@ -1429,14 +1454,19 @@ static void RCTMeasureLayout(RCTShadowView *view, | |
| withCallback:(RCTResponseSenderBlock)callback | ||
| errorCallback:(__unused RCTResponseSenderBlock)errorCallback) | ||
| { | ||
| if (_nextLayoutAnimation && ![config isEqualToDictionary:_nextLayoutAnimation.config]) { | ||
| RCTLogWarn(@"Warning: Overriding previous layout animation with new one before the first began:\n%@ -> %@.", _nextLayoutAnimation.config, config); | ||
| } | ||
| if (config[@"delete"] != nil) { | ||
| RCTLogError(@"LayoutAnimation only supports create and update right now. Config: %@", config); | ||
| RCTLayoutAnimation *currentAnimation = _layoutAnimation; | ||
|
|
||
| if (currentAnimation && ![config isEqualToDictionary:currentAnimation.config]) { | ||
| RCTLogWarn(@"Warning: Overriding previous layout animation with new one before the first began:\n%@ -> %@.", currentAnimation.config, config); | ||
| } | ||
| _nextLayoutAnimation = [[RCTLayoutAnimation alloc] initWithDictionary:config | ||
|
|
||
| RCTLayoutAnimation *nextLayoutAnimation = [[RCTLayoutAnimation alloc] initWithDictionary:config | ||
| callback:callback]; | ||
|
|
||
| // Set up next layout animation | ||
| [self addUIBlock:^(RCTUIManager *uiManager, __unused NSDictionary<NSNumber *, UIView *> *viewRegistry) { | ||
| uiManager->_layoutAnimation = nextLayoutAnimation; | ||
| }]; | ||
| } | ||
|
|
||
| static UIView *_jsResponder; | ||
|
|
||
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.
This is pretty sick how easy this is.