94 lines
1.9 KiB
Mathematica
Raw Normal View History

2015-03-31 10:44:16 -07:00
#import "BVLinearGradient.h"
2017-01-06 15:16:49 +00:00
#import <React/RCTConvert.h>
#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
2015-03-31 10:44:16 -07:00
@implementation BVLinearGradient
2017-04-07 14:20:17 +03:00
@synthesize start = _start;
@synthesize end = _end;
@synthesize colors = _colors;
- (void)invalidate
{
[self setNeedsDisplay];
}
- (void)setStart:(CGPoint)start
2015-03-31 10:44:16 -07:00
{
2017-04-07 14:20:17 +03:00
_start = start;
[self setNeedsDisplay];
2015-03-31 10:44:16 -07:00
}
2017-04-07 14:20:17 +03:00
- (void)setEnd:(CGPoint)end
2015-03-31 10:44:16 -07:00
{
2017-04-07 14:20:17 +03:00
_end = end;
[self setNeedsDisplay];
}
- (void)setLocations:(NSArray *)locations
{
2017-04-07 16:02:11 +03:00
// CGGradientCreateWithColors accepts locations as a C array
if (_locations) {
free(_locations);
_locations = NULL;
}
if (locations) {
NSUInteger count = [locations count];
_locations = malloc(sizeof(CGFloat *) * count);
for (int i = 0; i < count; ++i) {
_locations[i] = [[locations objectAtIndex:i] doubleValue];
}
}
2017-04-07 14:20:17 +03:00
[self setNeedsDisplay];
2015-03-31 10:44:16 -07:00
}
2017-04-07 16:02:11 +03:00
- (CGFloat *)locations
{
return _locations;
}
- (void)setColors:(NSArray *)colorStrings
2015-03-31 10:44:16 -07:00
{
NSMutableArray *colors = [NSMutableArray arrayWithCapacity:colorStrings.count];
for (NSString *colorString in colorStrings) {
[colors addObject:(id)[RCTConvert UIColor:colorString].CGColor];
}
2017-04-07 14:20:17 +03:00
_colors = colors;
[self setNeedsDisplay];
2015-03-31 10:44:16 -07:00
}
2017-04-07 14:20:17 +03:00
- (void)drawRect:(CGRect)rect
{
2017-04-07 14:20:17 +03:00
[super drawRect:rect];
2017-04-07 14:20:17 +03:00
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = UIGraphicsGetCurrentContext();
2017-04-07 14:20:17 +03:00
CGGradientRef gradient = CGGradientCreateWithColors(
colorSpace,
(CFArrayRef)self.colors,
2017-04-07 16:02:11 +03:00
self.locations
2017-04-07 14:20:17 +03:00
);
CGContextDrawLinearGradient(
context,
gradient,
CGPointMake(self.start.x * self.bounds.size.width, self.start.y * self.bounds.size.height),
CGPointMake(self.end.x * self.bounds.size.width, self.end.y * self.bounds.size.height),
kCGGradientDrawsBeforeStartLocation | kCGGradientDrawsAfterEndLocation
);
CGColorSpaceRelease(colorSpace);
CGGradientRelease(gradient);
}
2017-04-07 16:02:11 +03:00
- (void)dealloc
{
if (_locations) {
free(_locations);
}
}
2015-03-31 10:44:16 -07:00
@end