Skip to content
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

Fix targetGlobalOriginX/Y in custom layout animations #4052

Merged
merged 3 commits into from
Feb 21, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -32,6 +32,7 @@ class ReaLayoutAnimator extends LayoutAnimationController {
private volatile boolean mInitialized = false;
private final ReactApplicationContext mContext;
private final WeakReference<NativeViewHierarchyManager> mWeakNativeViewHierarchyManager;
private final ArrayList<View> viewsToSnapshot = new ArrayList<>();

ReaLayoutAnimator(
ReactApplicationContext context, NativeViewHierarchyManager nativeViewHierarchyManager) {
Expand Down Expand Up @@ -63,6 +64,22 @@ public boolean shouldAnimateLayout(View viewToAnimate) {
return (viewToAnimate.getParent() != null);
}

@Override
public void reset() {
super.reset();
// we have to make snapshots of the views after all of them have updated layouts
// to have correct global coordinates in the snapshots
// we do it here because React calls reset() method after all views have updated layouts
// and there is no semantically valid place to do it
for(View view : viewsToSnapshot) {
mAnimationsManager.onViewCreate(
view,
(ViewGroup) view.getParent(),
new Snapshot(view, mWeakNativeViewHierarchyManager.get()));
}
viewsToSnapshot.clear();
}

/**
* Update layout of given view, via immediate update or animation depending on the current batch
* layout animation configuration supplied during initialization. Handles create and update
Expand Down Expand Up @@ -92,10 +109,7 @@ public void applyLayoutUpdate(View view, int x, int y, int width, int height) {
}
view.layout(x, y, x + width, y + height);
if (view.getId() != -1) {
mAnimationsManager.onViewCreate(
view,
(ViewGroup) view.getParent(),
new Snapshot(view, mWeakNativeViewHierarchyManager.get()));
viewsToSnapshot.add(view);
}
return;
}
Expand Down
17 changes: 15 additions & 2 deletions ios/LayoutReanimation/REAUIManager.mm
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,8 @@ - (RCTViewManagerUIBlock)uiBlockWithLayoutUpdateForRootView:(RCTRootShadowView *

__block NSUInteger completionsCalled = 0;

NSMutableDictionary<NSNumber *, REASnapshot *> *snapshotsBefore = [NSMutableDictionary dictionary];

NSInteger index = 0;
for (NSNumber *reactTag in reactTags) {
RCTFrameData frameData = frameDataArray[index++];
Expand Down Expand Up @@ -259,6 +261,7 @@ - (RCTViewManagerUIBlock)uiBlockWithLayoutUpdateForRootView:(RCTRootShadowView *

// Reanimated changes /start
REASnapshot *snapshotBefore = isNew ? nil : [self->_animationsManager prepareSnapshotBeforeMountForView:view];
snapshotsBefore[reactTag] = snapshotBefore;
// Reanimated changes /end

if (creatingLayoutAnimation) {
Expand Down Expand Up @@ -305,19 +308,29 @@ - (RCTViewManagerUIBlock)uiBlockWithLayoutUpdateForRootView:(RCTRootShadowView *
[view reactSetFrame:frame];
completion(YES);
}
}

// Reanimated changes /start
index = 0;
for (NSNumber *reactTag in reactTags) {
RCTFrameData frameData = frameDataArray[index++];
UIView *view = viewRegistry[reactTag];
BOOL isNew = frameData.isNew;

REASnapshot *snapshotBefore = snapshotsBefore[reactTag];

// Reanimated changes /start
if (isNew || snapshotBefore != nil) {
[self->_animationsManager viewDidMount:view withBeforeSnapshot:snapshotBefore];
}
// Reanimated changes /end
}

// Clean up
// below line serves as this one uiManager->_layoutAnimationGroup = nil;, because we don't have access to the
// private field
[uiManager setNextLayoutAnimationGroup:nil];

[self->_animationsManager viewsDidLayout];
// Reanimated changes /end
};
}

Expand Down