Skip to content

Commit c2fd9b6

Browse files
committed
feat: added support for fade-in and fade-out animation types
1 parent f0d1dee commit c2fd9b6

File tree

7 files changed

+73
-31
lines changed

7 files changed

+73
-31
lines changed

README.md

+6-4
Original file line numberDiff line numberDiff line change
@@ -184,10 +184,12 @@ In this case you can use `resize="clip"` and `align="left-top"` to create a text
184184

185185
#### SharedElementAnimation
186186

187-
| Animation | Description |
188-
| --------- | ----------------------------------------------- |
189-
| `move` | Moves the start- element to the end position |
190-
| `fade` | Cross-fades between the start- and end elements |
187+
| Animation | Description |
188+
| ---------- | ------------------------------------------------------------------------------------- |
189+
| `move` | Moves the start- element to the end position |
190+
| `fade` | Cross-fades between the start- and end elements |
191+
| `fade-in` | Fade-in the end element coming from the start position (start-element is not visible) |
192+
| `fade-out` | Fade-out the start element to the end position (end-element is not visible) |
191193

192194

193195
#### SharedElementResize

android/src/main/java/com/ijzerenhein/sharedelement/RNSharedElementTransition.java

+37-19
Original file line numberDiff line numberDiff line change
@@ -231,28 +231,44 @@ private void updateLayout() {
231231
setTranslationX(parentLayout.left);
232232
setTranslationY(parentLayout.top);
233233

234+
// Determine opacity
235+
float startAlpha = 1.0f;
236+
float endAlpha = 1.0f;
237+
switch (mAnimation) {
238+
case MOVE:
239+
startAlpha = interpolatedStyle.opacity;
240+
endAlpha = 0.0f;
241+
break;
242+
case FADE:
243+
startAlpha = ((startStyle != null) ? startStyle.opacity : 1) * (1 - mNodePosition);
244+
endAlpha = ((endStyle != null) ? endStyle.opacity : 1) * mNodePosition;
245+
case FADE_IN:
246+
startAlpha = 0.0f;
247+
endAlpha = ((endStyle != null) ? endStyle.opacity : 1) * mNodePosition;
248+
break;
249+
case FADE_OUT:
250+
startAlpha = ((startStyle != null) ? startStyle.opacity : 1) * (1 - mNodePosition);
251+
endAlpha = 0.0f;
252+
break;
253+
}
254+
234255
// Render the start view
235-
boolean isCrossFade = mAnimation != RNSharedElementAnimation.MOVE;
236-
float startAlpha = !isCrossFade
237-
? interpolatedStyle.opacity
238-
: ((startStyle != null) ? startStyle.opacity : 1) * (1 - mNodePosition);
239-
mStartView.updateViewAndDrawable(
240-
interpolatedLayout,
241-
parentLayout,
242-
startContent,
243-
startLayout,
244-
interpolatedStyle,
245-
startAlpha,
246-
mResize,
247-
mAlign,
248-
mNodePosition
249-
);
256+
if (mAnimation != RNSharedElementAnimation.FADE_IN) {
257+
mStartView.updateViewAndDrawable(
258+
interpolatedLayout,
259+
parentLayout,
260+
startContent,
261+
startLayout,
262+
interpolatedStyle,
263+
startAlpha,
264+
mResize,
265+
mAlign,
266+
mNodePosition
267+
);
268+
}
250269

251270
// Render the end view as well for the "cross-fade" animations
252-
if (isCrossFade) {
253-
254-
// Render the end view
255-
float endAlpha = ((endStyle != null) ? endStyle.opacity : 1) * mNodePosition;
271+
if ((mAnimation == RNSharedElementAnimation.FADE) || (mAnimation == RNSharedElementAnimation.FADE_IN)) {
256272
mEndView.updateViewAndDrawable(
257273
interpolatedLayout,
258274
parentLayout,
@@ -285,6 +301,8 @@ private void updateNodeVisibility() {
285301
boolean hidden = mInitialLayoutPassCompleted &&
286302
(item.getStyle() != null) &&
287303
(item.getContent() != null);
304+
if (hidden && (mAnimation == RNSharedElementAnimation.FADE_IN) && item.getName().equals("start")) hidden = false;
305+
if (hidden && (mAnimation == RNSharedElementAnimation.FADE_OUT) && item.getName().equals("end")) hidden = false;
288306
item.setHidden(hidden);
289307
}
290308
}

android/src/main/java/com/ijzerenhein/sharedelement/RNSharedElementTypes.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
enum RNSharedElementAnimation {
44
MOVE(0),
5-
FADE(1);
5+
FADE(1),
6+
FADE_IN(2),
7+
FADE_OUT(3);
68

79
private final int value;
810
RNSharedElementAnimation(final int newValue) {value = newValue;}

ios/RNSharedElementTransition.m

+20-4
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,10 @@ - (void) setAlign:(RNSharedElementAlign)align
158158
- (void)updateNodeVisibility
159159
{
160160
for (RNSharedElementTransitionItem* item in _items) {
161-
item.hidden = _initialLayoutPassCompleted && item.style != nil && item.content != nil;
161+
BOOL hidden = _initialLayoutPassCompleted && item.style != nil && item.content != nil;
162+
if (hidden && (_animation == RNSharedElementAnimationFadeIn) && [item.name isEqualToString:@"startNode"]) hidden = NO;
163+
if (hidden && (_animation == RNSharedElementAnimationFadeOut) && [item.name isEqualToString:@"endNode"]) hidden = NO;
164+
item.hidden = hidden;
162165
}
163166
}
164167

@@ -539,9 +542,22 @@ - (void) updateStyle
539542
// Update end node
540543
contentView2.frame = endInterpolatedContentLayout;
541544

542-
// Cross-fade
543-
contentView1.layer.opacity = 1.0f - MIN(MAX(_nodePosition, 0.0f), 1.0f);
544-
contentView2.layer.opacity = MIN(MAX(_nodePosition, 0.0f), 1.0f);
545+
// Fade
546+
if (_animation == RNSharedElementAnimationFadeIn) {
547+
// Fade-in
548+
contentView1.layer.opacity = 0.0f;
549+
contentView2.layer.opacity = MIN(MAX(_nodePosition, 0.0f), 1.0f);
550+
}
551+
else if (_animation == RNSharedElementAnimationFadeOut) {
552+
// Fade-out
553+
contentView1.layer.opacity = 1.0f - MIN(MAX(_nodePosition, 0.0f), 1.0f);
554+
contentView2.layer.opacity = 0.0f;
555+
}
556+
else {
557+
// Cross-fade
558+
contentView1.layer.opacity = 1.0f - MIN(MAX(_nodePosition, 0.0f), 1.0f);
559+
contentView2.layer.opacity = MIN(MAX(_nodePosition, 0.0f), 1.0f);
560+
}
545561
}
546562

547563
// Fire events

ios/RNSharedElementTypes.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ typedef NS_ENUM(NSInteger, RNSharedElementContentType) {
1515

1616
typedef NS_ENUM(NSInteger, RNSharedElementAnimation) {
1717
RNSharedElementAnimationMove = 0,
18-
RNSharedElementAnimationFade = 1
18+
RNSharedElementAnimationFade = 1,
19+
RNSharedElementAnimationFadeIn = 2,
20+
RNSharedElementAnimationFadeOut = 3
1921
};
2022

2123
typedef NS_ENUM(NSInteger, RNSharedElementResize) {

src/SharedElementTransition.tsx

+3-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,9 @@ if (isAvailable) {
7979

8080
const NativeAnimationType = new Map<SharedElementAnimation, number>([
8181
["move", 0],
82-
["fade", 1]
82+
["fade", 1],
83+
["fade-in", 2],
84+
["fade-out", 3]
8385
]);
8486

8587
const NativeResizeType = new Map<SharedElementResize, number>([

src/types.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export type SharedElementNode = {
55
parentInstance: any;
66
};
77

8-
export type SharedElementAnimation = "move" | "fade";
8+
export type SharedElementAnimation = "move" | "fade" | "fade-in" | "fade-out";
99

1010
export type SharedElementResize = "auto" | "stretch" | "clip" | "none";
1111

0 commit comments

Comments
 (0)