From a32375003ed6eef994e6c84bc30ed7653653ec8f Mon Sep 17 00:00:00 2001 From: Xavier Schott Date: Thu, 12 Mar 2015 18:33:22 -0700 Subject: [PATCH] Fixed #2 - Images for TGPDiscreteSlider thumb, track and ticks not supported. Added tick images support. --- TGPControls/TGPCamelLabels.m | 10 ++++++++ TGPControls/TGPDiscreteSlider.h | 1 + TGPControls/TGPDiscreteSlider.m | 15 +++++++++++ TGPControls/TGPDiscreteSlider7.h | 2 ++ TGPControls/TGPDiscreteSlider7.m | 44 ++++++++++++++++++++------------ 5 files changed, 55 insertions(+), 17 deletions(-) diff --git a/TGPControls/TGPCamelLabels.m b/TGPControls/TGPCamelLabels.m index ae4b87b..4aabf84 100644 --- a/TGPControls/TGPCamelLabels.m +++ b/TGPControls/TGPCamelLabels.m @@ -32,4 +32,14 @@ @implementation TGPCamelLabels +@dynamic tickCount; +@dynamic ticksDistance; +@dynamic value; +@dynamic upFontName; +@dynamic upFontSize; +@dynamic upFontColor; +@dynamic downFontName; +@dynamic downFontSize; +@dynamic downFontColor; + @end diff --git a/TGPControls/TGPDiscreteSlider.h b/TGPControls/TGPDiscreteSlider.h index 7bb5755..2f422d5 100644 --- a/TGPControls/TGPDiscreteSlider.h +++ b/TGPControls/TGPDiscreteSlider.h @@ -36,6 +36,7 @@ IB_DESIGNABLE @property (nonatomic) IBInspectable int tickStyle; @property (nonatomic) IBInspectable CGSize tickSize; @property (nonatomic) IBInspectable int tickCount; +@property (nonatomic) IBInspectable NSString * tickImage; @property (nonatomic) IBInspectable int trackStyle; @property (nonatomic) IBInspectable CGFloat trackThickness; diff --git a/TGPControls/TGPDiscreteSlider.m b/TGPControls/TGPDiscreteSlider.m index aa6dcbf..2bf8da7 100644 --- a/TGPControls/TGPDiscreteSlider.m +++ b/TGPControls/TGPDiscreteSlider.m @@ -32,6 +32,21 @@ @implementation TGPDiscreteSlider +@dynamic tickStyle; +@dynamic tickSize; +@dynamic tickCount; +@dynamic tickImage; +@dynamic trackStyle; +@dynamic trackThickness; +@dynamic trackImage; +@dynamic thumbStyle; +@dynamic thumbSize; +@dynamic thumbColor; +@dynamic thumbImage; +@dynamic minimumValue; +@dynamic value; +@dynamic incrementValue; + #pragma mark properties - (void)setThumbSRadius:(CGFloat)thumbSRadius { diff --git a/TGPControls/TGPDiscreteSlider7.h b/TGPControls/TGPDiscreteSlider7.h index 913645b..59ae20f 100644 --- a/TGPControls/TGPDiscreteSlider7.h +++ b/TGPControls/TGPDiscreteSlider7.h @@ -52,6 +52,8 @@ typedef NS_ENUM(int, ComponentStyle) { @property (nonatomic, assign) CGSize tickSize; @property (nonatomic, assign) int tickCount; @property (nonatomic, readonly) CGFloat ticksDistance; +@property (nonatomic, strong) NSString * tickImage; + @property (nonatomic, assign) ComponentStyle trackStyle; @property (nonatomic, assign) CGFloat trackThickness; diff --git a/TGPControls/TGPDiscreteSlider7.m b/TGPControls/TGPDiscreteSlider7.m index 420f2cb..9eb26b7 100644 --- a/TGPControls/TGPDiscreteSlider7.m +++ b/TGPControls/TGPDiscreteSlider7.m @@ -70,6 +70,11 @@ static CGSize iosThumbShadowOffset = (CGSize){0, 3}; return (self.trackRectangle.size.width / segments); } +- (void)setTickImage:(NSString *)tickImage { + _tickImage = tickImage; + [self layoutTrack]; +} + - (void)setTrackStyle:(ComponentStyle)trackStyle { _trackStyle = trackStyle; [self layoutTrack]; @@ -227,13 +232,6 @@ static CGSize iosThumbShadowOffset = (CGSize){0, 3}; [self layoutTrack]; } -- (CGRect)centeredRect:(const CGSize)inside within:(const CGRect)outside { - return CGRectMake((outside.size.width/2) - (inside.width/2), - (outside.size.height/2) - (inside.height/2), - inside.width, - inside.height); -} - - (void)drawTrack { const CGContextRef ctx = UIGraphicsGetCurrentContext(); @@ -253,7 +251,10 @@ static CGSize iosThumbShadowOffset = (CGSize){0, 3}; if(imageName.length > 0) { UIImage * image = [UIImage imageNamed:imageName]; //[NSBundle bundleForClass:[self class]] if(image) { - CGRect centered = [self centeredRect:image.size within:self.frame]; + CGRect centered = CGRectMake((self.frame.size.width/2) - (image.size.width/2), + (self.frame.size.height/2) - (image.size.height/2), + image.size.width, + image.size.height); CGContextDrawImage(ctx, centered, image.CGImage); } } @@ -292,6 +293,22 @@ static CGSize iosThumbShadowOffset = (CGSize){0, 3}; CGContextAddRect(ctx, rectangle); break; + case ComponentStyleImage: { + // Draw image if exists + NSString * imageName = self.tickImage; + if(imageName.length > 0) { + UIImage * image = [UIImage imageNamed:imageName]; //[NSBundle bundleForClass:[self class]] + if(image) { + CGRect centered = CGRectMake(rectangle.origin.x + (rectangle.size.width/2) - (image.size.width/2), + rectangle.origin.y + (rectangle.size.height/2) - (image.size.height/2), + image.size.width, + image.size.height); + CGContextDrawImage(ctx, centered, image.CGImage); + } + } + break; + } + case ComponentStyleInvisible: case ComponentStyleIOS: default: @@ -300,7 +317,7 @@ static CGSize iosThumbShadowOffset = (CGSize){0, 3}; } } } - } // iOS UISlider does not have ticks + } // iOS UISlider aka ComponentStyleIOS does not have ticks CGContextSetFillColor(ctx, CGColorGetComponents([self.tintColor CGColor])); CGContextFillPath(ctx); @@ -351,14 +368,7 @@ static CGSize iosThumbShadowOffset = (CGSize){0, 3}; break; case ComponentStyleImage: { -// // Draw image if exists -// NSString * imageName = self.thumbImage; -// if(imageName.length > 0) { -// UIImage * image = [UIImage imageNamed:imageName]; //[NSBundle bundleForClass:[self class]] -// if(image) { -// self.thumbLayer.contents = (id)image.CGImage; -// } -// } + // image is set using layer.contents self.thumbLayer.backgroundColor = [[UIColor clearColor] CGColor]; self.thumbLayer.borderColor = [[UIColor clearColor] CGColor]; self.thumbLayer.borderWidth = 0.0;