Skip to content

Commit f0d1dee

Browse files
committed
fix(ios): fixed color interpolation when one was fully transparent
1 parent 71bd39a commit f0d1dee

3 files changed

+43
-31
lines changed

ios/RNSharedElementStyle.h

+2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727

2828
+ (NSString*) stringFromTransform:(CATransform3D) transform;
2929
+ (CATransform3D) getAbsoluteViewTransform:(UIView*) view;
30+
+ (UIColor*) getInterpolatedColor:(UIColor*)color1 color2:(UIColor*)color2 position:(CGFloat)position;
31+
+ (RNSharedElementStyle*) getInterpolatedStyle:(RNSharedElementStyle*)style1 style2:(RNSharedElementStyle*)style2 position:(CGFloat) position;
3032

3133
@end
3234

ios/RNSharedElementStyle.m

+40
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,44 @@ + (CATransform3D) getAbsoluteViewTransform:(UIView*) view
7575
return transform;
7676
}
7777

78+
+ (UIColor*) getInterpolatedColor:(UIColor*)color1 color2:(UIColor*)color2 position:(CGFloat)position
79+
{
80+
CGFloat red1, green1, blue1, alpha1;
81+
CGFloat red2, green2, blue2, alpha2;
82+
[color1 getRed:&red1 green:&green1 blue:&blue1 alpha:&alpha1];
83+
[color2 getRed:&red2 green:&green2 blue:&blue2 alpha:&alpha2];
84+
CGFloat alpha = alpha1 + ((alpha2 - alpha1) * position);
85+
CGFloat red = red1 + ((red2 - red1) * position);
86+
CGFloat green = green1 + ((green2 - green1) * position);
87+
CGFloat blue = blue1 + ((blue2 - blue1) * position);
88+
if ((alpha1 == 0.0f) && (alpha2 != 0.0f)) {
89+
red = red2;
90+
green = green2;
91+
blue = blue2;
92+
} else if ((alpha2 == 0.0f) && (alpha1 != 0.0f)) {
93+
red = red1;
94+
green = green1;
95+
blue = blue1;
96+
}
97+
return [UIColor colorWithRed:red green:green blue:blue alpha:alpha];
98+
}
99+
100+
+ (RNSharedElementStyle*) getInterpolatedStyle:(RNSharedElementStyle*)style1 style2:(RNSharedElementStyle*)style2 position:(CGFloat) position
101+
{
102+
RNSharedElementStyle* style = [[RNSharedElementStyle alloc]init];
103+
style.opacity = style1.opacity + ((style2.opacity - style1.opacity) * position);
104+
style.cornerRadius = style1.cornerRadius + ((style2.cornerRadius - style1.cornerRadius) * position);
105+
style.borderWidth = style1.borderWidth + ((style2.borderWidth - style1.borderWidth) * position);
106+
style.borderColor = [RNSharedElementStyle getInterpolatedColor:style1.borderColor color2:style2.borderColor position:position];
107+
style.backgroundColor = [RNSharedElementStyle getInterpolatedColor:style1.backgroundColor color2:style2.backgroundColor position:position];
108+
style.shadowOpacity = style1.shadowOpacity + ((style2.shadowOpacity - style1.shadowOpacity) * position);
109+
style.shadowRadius = style1.shadowRadius + ((style2.shadowRadius - style1.shadowRadius) * position);
110+
style.shadowOffset = CGSizeMake(
111+
style1.shadowOffset.width + ((style2.shadowOffset.width - style1.shadowOffset.width) * position),
112+
style1.shadowOffset.height + ((style2.shadowOffset.height - style1.shadowOffset.height) * position)
113+
);
114+
style.shadowColor = [RNSharedElementStyle getInterpolatedColor:style1.shadowColor color2:style2.shadowColor position:position];
115+
return style;
116+
}
117+
78118
@end

ios/RNSharedElementTransition.m

+1-31
Original file line numberDiff line numberDiff line change
@@ -247,18 +247,6 @@ - (CGRect)normalizeLayout:(CGRect)layout ancestor:(RNSharedElementTransitionItem
247247
return [self.superview convertRect:layout fromView:nil];
248248
}
249249

250-
- (UIColor*) getInterpolatedColor:(UIColor*)color1 color2:(UIColor*)color2 position:(CGFloat)position
251-
{
252-
CGFloat red1, green1, blue1, alpha1;
253-
CGFloat red2, green2, blue2, alpha2;
254-
[color1 getRed:&red1 green:&green1 blue:&blue1 alpha:&alpha1];
255-
[color2 getRed:&red2 green:&green2 blue:&blue2 alpha:&alpha2];
256-
return [UIColor colorWithRed:red1 + ((red2 - red1) * position)
257-
green:green1 + ((green2 - green1) * position)
258-
blue:blue1 + ((blue2 - blue1) * position)
259-
alpha:alpha1 + ((alpha2 - alpha1) * position)];
260-
}
261-
262250
- (CGRect) getInterpolatedLayout:(CGRect)layout1 layout2:(CGRect)layout2 position:(CGFloat) position
263251
{
264252
return CGRectMake(
@@ -322,24 +310,6 @@ - (UIEdgeInsets) getInterpolatedClipInsets:(CGRect)interpolatedLayout startClipI
322310
return clipInsets;
323311
}
324312

325-
- (RNSharedElementStyle*) getInterpolatedStyle:(RNSharedElementStyle*)style1 style2:(RNSharedElementStyle*)style2 position:(CGFloat) position
326-
{
327-
RNSharedElementStyle* style = [[RNSharedElementStyle alloc]init];
328-
style.opacity = style1.opacity + ((style2.opacity - style1.opacity) * position);
329-
style.cornerRadius = style1.cornerRadius + ((style2.cornerRadius - style1.cornerRadius) * position);
330-
style.borderWidth = style1.borderWidth + ((style2.borderWidth - style1.borderWidth) * position);
331-
style.borderColor = [self getInterpolatedColor:style1.borderColor color2:style2.borderColor position:position];
332-
style.backgroundColor = [self getInterpolatedColor:style1.backgroundColor color2:style2.backgroundColor position:position];
333-
style.shadowOpacity = style1.shadowOpacity + ((style2.shadowOpacity - style1.shadowOpacity) * position);
334-
style.shadowRadius = style1.shadowRadius + ((style2.shadowRadius - style1.shadowRadius) * position);
335-
style.shadowOffset = CGSizeMake(
336-
style1.shadowOffset.width + ((style2.shadowOffset.width - style1.shadowOffset.width) * position),
337-
style1.shadowOffset.height + ((style2.shadowOffset.height - style1.shadowOffset.height) * position)
338-
);
339-
style.shadowColor = [self getInterpolatedColor:style1.shadowColor color2:style2.shadowColor position:position];
340-
return style;
341-
}
342-
343313
- (void) applyStyle:(RNSharedElementStyle*)style layer:(CALayer*)layer
344314
{
345315
layer.opacity = style.opacity;
@@ -411,7 +381,7 @@ - (void) updateStyle
411381
UIEdgeInsets interpolatedClipInsets;
412382
if (!startStyle && !endStyle) return;
413383
if (startStyle && endStyle) {
414-
interpolatedStyle = [self getInterpolatedStyle:startStyle style2:endStyle position:_nodePosition];
384+
interpolatedStyle = [RNSharedElementStyle getInterpolatedStyle:startStyle style2:endStyle position:_nodePosition];
415385
interpolatedLayout = [self getInterpolatedLayout:startLayout layout2:endLayout position:_nodePosition];
416386
interpolatedClipInsets = [self getInterpolatedClipInsets:interpolatedLayout startClipInsets:startClipInsets startVisibleLayout:startVisibleLayout endClipInsets:endClipInsets endVisibleLayout:endVisibleLayout];
417387
interpolatedContentLayout = [self getInterpolatedLayout:startContentLayout layout2:endContentLayout position:_nodePosition];

0 commit comments

Comments
 (0)