From 9759eac38bf7dafb57b9196593cd971a12370448 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E5=8A=B2=E9=B9=8F?= Date: Sat, 9 May 2020 19:39:17 +0800 Subject: [PATCH] iOS: implements locations in gradient color --- doric-iOS/Pod/Classes/Shader/DoricViewNode.m | 36 ++++++++++++++++---- 1 file changed, 29 insertions(+), 7 deletions(-) diff --git a/doric-iOS/Pod/Classes/Shader/DoricViewNode.m b/doric-iOS/Pod/Classes/Shader/DoricViewNode.m index e1bb7951..838c2b81 100644 --- a/doric-iOS/Pod/Classes/Shader/DoricViewNode.m +++ b/doric-iOS/Pod/Classes/Shader/DoricViewNode.m @@ -317,6 +317,7 @@ + (__kindof DoricViewNode *)create:(DoricContext *)context withType:(NSString *) } - (UIImage *)gradientImageFromColors:(NSArray *)colors + locations:(CGFloat *)locations startPoint:(CGPoint)startPoint endPoint:(CGPoint)endPoint imgSize:(CGSize)imgSize { @@ -324,7 +325,7 @@ - (UIImage *)gradientImageFromColors:(NSArray *)colors CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSaveGState(context); CGColorSpaceRef colorSpace = CGColorGetColorSpace((__bridge CGColorRef) colors.lastObject); - CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef) colors, NULL); + CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef) colors, locations); CGPoint start = (CGPoint) {startPoint.x * imgSize.width, startPoint.y * imgSize.height}; CGPoint end = (CGPoint) {endPoint.x * imgSize.width, endPoint.y * imgSize.height}; CGContextDrawLinearGradient(context, gradient, start, end, kCGGradientDrawsBeforeStartLocation | kCGGradientDrawsAfterEndLocation); @@ -341,12 +342,18 @@ - (void)requestLayout { return; } self.gradientSize = self.view.frame.size; + NSMutableArray *colors = [[NSMutableArray alloc] init]; + NSMutableArray *arrayLocations = nil; if ([dict objectForKey:@"colors"] != nil) { - NSMutableArray *array = [dict mutableArrayValueForKey:@"colors"]; - [array forEach:^(id obj) { + NSMutableArray *arrayColors = [dict mutableArrayValueForKey:@"colors"]; + [arrayColors forEach:^(id obj) { [colors addObject:(__bridge id) DoricColor(obj).CGColor]; }]; + if ([dict objectForKey:@"locations"] != nil) { + arrayLocations = [dict mutableArrayValueForKey:@"locations"]; + } + } else { if ([dict objectForKey:@"start"] != nil && [dict objectForKey:@"end"] != nil) { UIColor *start = DoricColor(dict[@"start"]); @@ -385,10 +392,25 @@ - (void)requestLayout { startPoint = CGPointMake(0, 0); endPoint = CGPointMake(0, 1); } - UIImage *gradientImage = [self gradientImageFromColors:colors - startPoint:startPoint - endPoint:endPoint - imgSize:self.gradientSize]; + + UIImage *gradientImage; + if (arrayLocations != nil) { + CGFloat locations[arrayLocations.count]; + for (int i = 0; i != arrayLocations.count; i++) { + locations[i] = [arrayLocations[i] floatValue]; + } + gradientImage = [self gradientImageFromColors:colors + locations:locations + startPoint:startPoint + endPoint:endPoint + imgSize:self.gradientSize]; + } else { + gradientImage = [self gradientImageFromColors:colors + locations:NULL + startPoint:startPoint + endPoint:endPoint + imgSize:self.gradientSize]; + } self.view.backgroundColor = [UIColor colorWithPatternImage:gradientImage]; }]; }