From 438a1b9e540b52c7b477ac87def5776ce5b63f6a Mon Sep 17 00:00:00 2001 From: Xavier Schott Date: Mon, 7 Dec 2015 00:20:03 -0800 Subject: [PATCH] Fixed #10 - TGPDiscreteSlider: Tint colour sets the track colour, rather than the 'fill' color --- TGPControls/TGPDiscreteSlider7.m | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/TGPControls/TGPDiscreteSlider7.m b/TGPControls/TGPDiscreteSlider7.m index d185790..a57c447 100644 --- a/TGPControls/TGPDiscreteSlider7.m +++ b/TGPControls/TGPDiscreteSlider7.m @@ -43,6 +43,7 @@ static CGSize iosThumbShadowOffset = (CGSize){0, 3}; @property (nonatomic) CALayer * leftTrackLayer; @property (nonatomic) CALayer * rightTrackLayer; @property (nonatomic) CALayer * trackLayer; +@property (nonatomic) CALayer * ticksLayer; @property (nonatomic) CGRect trackRectangle; @property (nonatomic) BOOL touchedInside; @end @@ -208,6 +209,7 @@ static CGSize iosThumbShadowOffset = (CGSize){0, 3}; - (void)drawRect:(CGRect)rect { [self drawTrack]; + [self drawTicks]; [self drawThumb]; } @@ -252,6 +254,10 @@ static CGSize iosThumbShadowOffset = (CGSize){0, 3}; _rightTrackLayer.backgroundColor = [self.maximumTrackTintColor CGColor]; [self.trackLayer addSublayer:self.rightTrackLayer]; + // Ticks in between track and thumb + _ticksLayer = [CALayer layer]; + [self.layer addSublayer:self.ticksLayer]; + // The thumb is its own CALayer, which brings in free animation _thumbLayer = [CALayer layer]; [self.layer addSublayer:self.thumbLayer]; @@ -260,8 +266,11 @@ static CGSize iosThumbShadowOffset = (CGSize){0, 3}; [self layoutTrack]; } -- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx { - [super drawLayer:layer inContext:ctx]; +- (void)drawTicks { + self.ticksLayer.frame = self.bounds; + self.ticksLayer.backgroundColor = [self.tintColor CGColor]; + + UIBezierPath *path = [UIBezierPath bezierPath]; switch (self.tickStyle) { case ComponentStyleRounded: @@ -277,18 +286,13 @@ static CGSize iosThumbShadowOffset = (CGSize){0, 3}; self.tickSize.width, self.tickSize.height); switch(self.tickStyle) { case ComponentStyleRounded: { - UIBezierPath * path = [UIBezierPath bezierPathWithRoundedRect:rectangle - cornerRadius:rectangle.size.height/2]; - CGContextAddPath(ctx, [path CGPath]) ; - CGContextSetFillColorWithColor(ctx, [self.tintColor CGColor]); - CGContextFillPath(ctx); + [path appendPath:[UIBezierPath bezierPathWithRoundedRect:rectangle + cornerRadius:rectangle.size.height/2]]; break; } case ComponentStyleRectangular: - CGContextAddRect(ctx, rectangle); - CGContextSetFillColorWithColor(ctx, [self.tintColor CGColor]); - CGContextFillPath(ctx); + [path appendPath:[UIBezierPath bezierPathWithRect:rectangle]]; break; case ComponentStyleImage: { @@ -301,6 +305,7 @@ static CGSize iosThumbShadowOffset = (CGSize){0, 3}; rectangle.origin.y + (rectangle.size.height/2) - (image.size.height/2), image.size.width, image.size.height); + const CGContextRef ctx = UIGraphicsGetCurrentContext(); CGContextDrawImage(ctx, centered, image.CGImage); } } @@ -323,8 +328,12 @@ static CGSize iosThumbShadowOffset = (CGSize){0, 3}; default: // Nothing to draw break; - } + + CAShapeLayer *maskLayer = [CAShapeLayer layer]; + maskLayer.frame = self.trackLayer.bounds; + maskLayer.path = path.CGPath; + self.ticksLayer.mask = maskLayer; } - (void)drawTrack {