iOS: implements locations in gradient color
This commit is contained in:
parent
506f89930e
commit
9759eac38b
@ -317,6 +317,7 @@ + (__kindof DoricViewNode *)create:(DoricContext *)context withType:(NSString *)
|
|||||||
}
|
}
|
||||||
|
|
||||||
- (UIImage *)gradientImageFromColors:(NSArray *)colors
|
- (UIImage *)gradientImageFromColors:(NSArray *)colors
|
||||||
|
locations:(CGFloat *)locations
|
||||||
startPoint:(CGPoint)startPoint
|
startPoint:(CGPoint)startPoint
|
||||||
endPoint:(CGPoint)endPoint
|
endPoint:(CGPoint)endPoint
|
||||||
imgSize:(CGSize)imgSize {
|
imgSize:(CGSize)imgSize {
|
||||||
@ -324,7 +325,7 @@ - (UIImage *)gradientImageFromColors:(NSArray *)colors
|
|||||||
CGContextRef context = UIGraphicsGetCurrentContext();
|
CGContextRef context = UIGraphicsGetCurrentContext();
|
||||||
CGContextSaveGState(context);
|
CGContextSaveGState(context);
|
||||||
CGColorSpaceRef colorSpace = CGColorGetColorSpace((__bridge CGColorRef) colors.lastObject);
|
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 start = (CGPoint) {startPoint.x * imgSize.width, startPoint.y * imgSize.height};
|
||||||
CGPoint end = (CGPoint) {endPoint.x * imgSize.width, endPoint.y * imgSize.height};
|
CGPoint end = (CGPoint) {endPoint.x * imgSize.width, endPoint.y * imgSize.height};
|
||||||
CGContextDrawLinearGradient(context, gradient, start, end, kCGGradientDrawsBeforeStartLocation | kCGGradientDrawsAfterEndLocation);
|
CGContextDrawLinearGradient(context, gradient, start, end, kCGGradientDrawsBeforeStartLocation | kCGGradientDrawsAfterEndLocation);
|
||||||
@ -341,12 +342,18 @@ - (void)requestLayout {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
self.gradientSize = self.view.frame.size;
|
self.gradientSize = self.view.frame.size;
|
||||||
|
|
||||||
NSMutableArray *colors = [[NSMutableArray alloc] init];
|
NSMutableArray *colors = [[NSMutableArray alloc] init];
|
||||||
|
NSMutableArray *arrayLocations = nil;
|
||||||
if ([dict objectForKey:@"colors"] != nil) {
|
if ([dict objectForKey:@"colors"] != nil) {
|
||||||
NSMutableArray *array = [dict mutableArrayValueForKey:@"colors"];
|
NSMutableArray *arrayColors = [dict mutableArrayValueForKey:@"colors"];
|
||||||
[array forEach:^(id obj) {
|
[arrayColors forEach:^(id obj) {
|
||||||
[colors addObject:(__bridge id) DoricColor(obj).CGColor];
|
[colors addObject:(__bridge id) DoricColor(obj).CGColor];
|
||||||
}];
|
}];
|
||||||
|
if ([dict objectForKey:@"locations"] != nil) {
|
||||||
|
arrayLocations = [dict mutableArrayValueForKey:@"locations"];
|
||||||
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
if ([dict objectForKey:@"start"] != nil && [dict objectForKey:@"end"] != nil) {
|
if ([dict objectForKey:@"start"] != nil && [dict objectForKey:@"end"] != nil) {
|
||||||
UIColor *start = DoricColor(dict[@"start"]);
|
UIColor *start = DoricColor(dict[@"start"]);
|
||||||
@ -385,10 +392,25 @@ - (void)requestLayout {
|
|||||||
startPoint = CGPointMake(0, 0);
|
startPoint = CGPointMake(0, 0);
|
||||||
endPoint = CGPointMake(0, 1);
|
endPoint = CGPointMake(0, 1);
|
||||||
}
|
}
|
||||||
UIImage *gradientImage = [self gradientImageFromColors:colors
|
|
||||||
|
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
|
startPoint:startPoint
|
||||||
endPoint:endPoint
|
endPoint:endPoint
|
||||||
imgSize:self.gradientSize];
|
imgSize:self.gradientSize];
|
||||||
|
} else {
|
||||||
|
gradientImage = [self gradientImageFromColors:colors
|
||||||
|
locations:NULL
|
||||||
|
startPoint:startPoint
|
||||||
|
endPoint:endPoint
|
||||||
|
imgSize:self.gradientSize];
|
||||||
|
}
|
||||||
self.view.backgroundColor = [UIColor colorWithPatternImage:gradientImage];
|
self.view.backgroundColor = [UIColor colorWithPatternImage:gradientImage];
|
||||||
}];
|
}];
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user