better locaitons handling

This commit is contained in:
Dmitry Gladkov 2017-04-07 16:02:11 +03:00
parent 71dbf39f4c
commit af78c3e376
2 changed files with 31 additions and 12 deletions

View File

@ -1,9 +1,13 @@
#import <UIKit/UIKit.h>
@interface BVLinearGradient : UIView
@interface BVLinearGradient : UIView {
CGFloat *_locations;
}
- (CGFloat *)locations;
- (void)setLocations:(NSArray *)colorStrings;
@property (nonatomic, retain) NSArray *colors;
@property (nonatomic, retain) NSArray *locations;
@property (nonatomic, assign) CGPoint start;
@property (nonatomic, assign) CGPoint end;

View File

@ -7,7 +7,6 @@
@synthesize start = _start;
@synthesize end = _end;
@synthesize locations = _locations;
@synthesize colors = _colors;
- (void)invalidate
@ -29,10 +28,26 @@
- (void)setLocations:(NSArray *)locations
{
_locations = locations;
// 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];
}
}
[self setNeedsDisplay];
}
- (CGFloat *)locations
{
return _locations;
}
- (void)setColors:(NSArray *)colorStrings
{
NSMutableArray *colors = [NSMutableArray arrayWithCapacity:colorStrings.count];
@ -50,17 +65,10 @@
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = UIGraphicsGetCurrentContext();
// CGGradientCreateWithColors accepts locations as a C array
int count = [self.locations count];
CGFloat locArray[count];
for (int i = 0; i < count; ++i) {
locArray[i] = [[self.locations objectAtIndex:i] doubleValue];
}
CGGradientRef gradient = CGGradientCreateWithColors(
colorSpace,
(CFArrayRef)self.colors,
locArray
self.locations
);
CGContextDrawLinearGradient(
@ -75,4 +83,11 @@
CGGradientRelease(gradient);
}
- (void)dealloc
{
if (_locations) {
free(_locations);
}
}
@end