-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Unify LinearGradient behavior across platforms (expo#2624)
* Add EXLinearGradientLayer to unify gradient rendering across platforms - copied from react-native-linear-gradient - moved to LinearGradient directory under Components * Clamp the gradient * Add an example of this confusing gradient to NCL * Add CHANGELOG entry
- Loading branch information
Showing
11 changed files
with
175 additions
and
16 deletions.
There are no files selected for viewing
This file contains 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 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains 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 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
File renamed without changes.
This file contains 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
14 changes: 14 additions & 0 deletions
14
ios/Exponent/Versioned/Core/Api/Components/LinearGradient/EXLinearGradientLayer.h
This file contains 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,14 @@ | ||
// Copyright 2018-present 650 Industries. All rights reserved. | ||
|
||
#import <Foundation/Foundation.h> | ||
#import <QuartzCore/QuartzCore.h> | ||
#import <UIKit/UIKit.h> | ||
|
||
@interface EXLinearGradientLayer : CALayer | ||
|
||
@property (nullable, nonatomic, copy) NSArray<UIColor *> *colors; | ||
@property (nullable, nonatomic, copy) NSArray<NSNumber *> *locations; | ||
@property (nonatomic) CGPoint startPoint; | ||
@property (nonatomic) CGPoint endPoint; | ||
|
||
@end |
110 changes: 110 additions & 0 deletions
110
ios/Exponent/Versioned/Core/Api/Components/LinearGradient/EXLinearGradientLayer.m
This file contains 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,110 @@ | ||
// Copyright 2018-present 650 Industries. All rights reserved. | ||
|
||
#import "EXLinearGradientLayer.h" | ||
|
||
@implementation EXLinearGradientLayer | ||
|
||
- (instancetype)init | ||
{ | ||
self = [super init]; | ||
|
||
if (self) { | ||
self.needsDisplayOnBoundsChange = YES; | ||
self.masksToBounds = YES; | ||
_startPoint = CGPointMake(0.5, 0.0); | ||
_endPoint = CGPointMake(0.5, 1.0); | ||
} | ||
|
||
return self; | ||
} | ||
|
||
- (void)setColors:(NSArray<id> *)colors | ||
{ | ||
_colors = colors; | ||
[self setNeedsDisplay]; | ||
} | ||
|
||
- (void)setLocations:(NSArray<NSNumber *> *)locations | ||
{ | ||
_locations = locations; | ||
[self setNeedsDisplay]; | ||
} | ||
|
||
- (void)setStartPoint:(CGPoint)startPoint | ||
{ | ||
_startPoint = startPoint; | ||
[self setNeedsDisplay]; | ||
} | ||
|
||
- (void)setEndPoint:(CGPoint)endPoint | ||
{ | ||
_endPoint = endPoint; | ||
[self setNeedsDisplay]; | ||
} | ||
|
||
- (void)display { | ||
[super display]; | ||
|
||
BOOL hasAlpha = NO; | ||
|
||
for (NSInteger i = 0; i < self.colors.count; i++) { | ||
hasAlpha = hasAlpha || CGColorGetAlpha(self.colors[i].CGColor) < 1.0; | ||
} | ||
|
||
UIGraphicsBeginImageContextWithOptions(self.bounds.size, !hasAlpha, 0.0); | ||
CGContextRef ref = UIGraphicsGetCurrentContext(); | ||
[self drawInContext:ref]; | ||
|
||
UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); | ||
self.contents = (__bridge id _Nullable)(image.CGImage); | ||
self.contentsScale = image.scale; | ||
|
||
UIGraphicsEndImageContext(); | ||
} | ||
|
||
- (void)drawInContext:(CGContextRef)ctx | ||
{ | ||
[super drawInContext:ctx]; | ||
|
||
CGContextSaveGState(ctx); | ||
|
||
CGSize size = self.bounds.size; | ||
if (!self.colors || self.colors.count == 0 || size.width == 0.0 || size.height == 0.0) | ||
return; | ||
|
||
|
||
CGFloat *locations = nil; | ||
|
||
locations = malloc(sizeof(CGFloat) * self.colors.count); | ||
|
||
for (NSInteger i = 0; i < self.colors.count; i++) { | ||
if (self.locations.count > i) { | ||
locations[i] = self.locations[i].floatValue; | ||
} else { | ||
locations[i] = (1.0 / (self.colors.count - 1)) * i; | ||
} | ||
} | ||
|
||
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); | ||
NSMutableArray *colors = [[NSMutableArray alloc] initWithCapacity:self.colors.count]; | ||
for (UIColor *color in self.colors) { | ||
[colors addObject:(id)color.CGColor]; | ||
} | ||
|
||
CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (CFArrayRef)colors, locations); | ||
|
||
free(locations); | ||
|
||
CGPoint start = self.startPoint, end = self.endPoint; | ||
|
||
CGContextDrawLinearGradient(ctx, gradient, | ||
CGPointMake(start.x * size.width, start.y * size.height), | ||
CGPointMake(end.x * size.width, end.y * size.height), | ||
kCGGradientDrawsBeforeStartLocation | kCGGradientDrawsAfterEndLocation); | ||
CGGradientRelease(gradient); | ||
CGColorSpaceRelease(colorSpace); | ||
|
||
CGContextRestoreGState(ctx); | ||
} | ||
|
||
@end |
File renamed without changes.
File renamed without changes.