Skip to content

Commit

Permalink
fix(ios): fixed border/background anim on >= RN60
Browse files Browse the repository at this point in the history
  • Loading branch information
IjzerenHein committed Dec 8, 2019
1 parent 15fdefb commit 17ef283
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 16 deletions.
18 changes: 2 additions & 16 deletions ios/RNSharedElementNode.m
Original file line number Diff line number Diff line change
Expand Up @@ -289,29 +289,15 @@ - (void) updateStyle
if (CGRectIsEmpty(layout)) return;

// Create style
RNSharedElementStyle* style = [[RNSharedElementStyle alloc]init];
CALayer* layer = view.layer;
style.view = view;
RNSharedElementStyle* style = [[RNSharedElementStyle alloc]initWithView:view];
style.layout = layout;
style.size = view.bounds.size;
style.transform = [RNSharedElementStyle getAbsoluteViewTransform:view];
if ([RNSharedElementContent isKindOfImageView:view]) {
UIImageView* imageView = [RNSharedElementContent imageViewFromView:view];
style.contentMode = imageView.contentMode;
} else {
style.contentMode = view.contentMode;
}
style.opacity = layer.opacity;
style.cornerRadius = layer.cornerRadius;
style.borderWidth = layer.borderWidth;
style.borderColor = layer.borderColor ? [UIColor colorWithCGColor:layer.borderColor] : [UIColor clearColor];
style.backgroundColor = layer.backgroundColor ? [UIColor colorWithCGColor:layer.backgroundColor] : [UIColor clearColor];
style.shadowColor = layer.shadowColor ? [UIColor colorWithCGColor:layer.shadowColor] : [UIColor clearColor];
style.shadowOffset = layer.shadowOffset;
style.shadowRadius = layer.shadowRadius;
style.shadowOpacity = layer.shadowOpacity;

/*NSLog(@"Style fetched: %@, realSize: %@, opacity: %lf, transform: %@", NSStringFromCGRect(layout), NSStringFromCGSize(view.bounds.size), style.opacity, [RNSharedElementStyle stringFromTransform:style.transform]);*/
// NSLog(@"Style fetched: %@, realSize: %@, opacity: %lf, transform: %@, borderWidth: %lf", NSStringFromCGRect(layout), NSStringFromCGSize(style.size), style.opacity, [RNSharedElementStyle stringFromTransform:style.transform], style.borderWidth);

_styleCache = style;

Expand Down
1 change: 1 addition & 0 deletions ios/RNSharedElementStyle.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
@property (nonatomic, assign) CGSize shadowOffset;
@property (nonatomic, assign) UIColor* shadowColor;
- (instancetype)init;
- (instancetype)initWithView:(UIView*) view;

+ (NSString*) stringFromTransform:(CATransform3D) transform;
+ (CATransform3D) getAbsoluteViewTransform:(UIView*) view;
Expand Down
33 changes: 33 additions & 0 deletions ios/RNSharedElementStyle.m
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
//

#import "RNSharedElementStyle.h"
#import <React/RCTView.h>

@implementation RNSharedElementStyle
{
Expand All @@ -17,6 +18,38 @@ - (instancetype)init
return self;
}

- (instancetype)initWithView:(UIView*) view
{
_view = view;
_size = view.bounds.size;
_transform = [RNSharedElementStyle getAbsoluteViewTransform:view];

// Set base props from style
CALayer* layer = view.layer;
_opacity = layer.opacity;
_cornerRadius = layer.cornerRadius;
_borderWidth = layer.borderWidth;
_borderColor = layer.borderColor ? [UIColor colorWithCGColor:layer.borderColor] : [UIColor clearColor];
_backgroundColor = layer.backgroundColor ? [UIColor colorWithCGColor:layer.backgroundColor] : [UIColor clearColor];
_shadowColor = layer.shadowColor ? [UIColor colorWithCGColor:layer.shadowColor] : [UIColor clearColor];
_shadowOffset = layer.shadowOffset;
_shadowRadius = layer.shadowRadius;
_shadowOpacity = layer.shadowOpacity;

// On RN60 and beyond, certain styles are not immediately applied to the view/layer
// when a borderWidth is set on the view. Therefore, as a fail-safe we also try to
// get the props from the RCTView directly, when possible.
if ([view isKindOfClass:[RCTView class]]) {
RCTView* rctView = (RCTView*) view;
_cornerRadius = rctView.borderRadius;
_borderColor = rctView.borderColor ? [UIColor colorWithCGColor:rctView.borderColor] : [UIColor clearColor];
_borderWidth = rctView.borderWidth >= 0.0f ? rctView.borderWidth : 0.0f;
_backgroundColor = rctView.backgroundColor ? rctView.backgroundColor : [UIColor clearColor];
}

return self;
}

- (void) setBackgroundColor:(UIColor*)backgroundColor {
_backgroundColor = backgroundColor;
}
Expand Down

0 comments on commit 17ef283

Please sign in to comment.