diff --git a/BVLinearGradient/BVLinearGradient.h b/BVLinearGradient/BVLinearGradient.h index 322d07c..ac0dad9 100644 --- a/BVLinearGradient/BVLinearGradient.h +++ b/BVLinearGradient/BVLinearGradient.h @@ -1,9 +1,13 @@ #import -@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; diff --git a/BVLinearGradient/BVLinearGradient.m b/BVLinearGradient/BVLinearGradient.m index 3fc3379..369e9e9 100644 --- a/BVLinearGradient/BVLinearGradient.m +++ b/BVLinearGradient/BVLinearGradient.m @@ -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