From 58a2e01f28f481aed2b4ae1de5bf8c6950516e24 Mon Sep 17 00:00:00 2001 From: Randall Wood Date: Tue, 18 Oct 2011 05:03:47 +0430 Subject: [PATCH] Add ability to use images for On and Off states. --- DCRoundSwitch/DCRoundSwitch.h | 2 + DCRoundSwitch/DCRoundSwitch.m | 26 +++++++++- DCRoundSwitch/DCRoundSwitchToggleLayer.h | 3 ++ DCRoundSwitch/DCRoundSwitchToggleLayer.m | 64 +++++++++++++++++------- 4 files changed, 76 insertions(+), 19 deletions(-) diff --git a/DCRoundSwitch/DCRoundSwitch.h b/DCRoundSwitch/DCRoundSwitch.h index 3d8d616..18ac459 100644 --- a/DCRoundSwitch/DCRoundSwitch.h +++ b/DCRoundSwitch/DCRoundSwitch.h @@ -30,6 +30,8 @@ @property (nonatomic, getter=isOn) BOOL on; // default: NO @property (nonatomic, copy) NSString *onText; // default: 'ON' - not automatically localized! @property (nonatomic, copy) NSString *offText; // default: 'OFF' - not automatically localized! +@property (nonatomic, copy) UIImage *onImage; +@property (nonatomic, copy) UIImage *offImage; - (void)setOn:(BOOL)newOn animated:(BOOL)animated; - (void)setOn:(BOOL)newOn animated:(BOOL)animated ignoreControlEvents:(BOOL)ignoreControlEvents; diff --git a/DCRoundSwitch/DCRoundSwitch.m b/DCRoundSwitch/DCRoundSwitch.m index 5c1435a..91ba7cb 100644 --- a/DCRoundSwitch/DCRoundSwitch.m +++ b/DCRoundSwitch/DCRoundSwitch.m @@ -24,7 +24,7 @@ - (void)positionLayersAndMask; @end @implementation DCRoundSwitch -@synthesize on, onText, offText; +@synthesize on, onText, offText, onImage, offImage; @synthesize onTintColor; #pragma mark - @@ -35,6 +35,8 @@ - (void)dealloc [onTintColor release]; [onText release]; [offText release]; + [onImage release]; + [offImage release]; [super dealloc]; } @@ -427,4 +429,26 @@ - (void)setOffText:(NSString *)newOffText } } +- (void)setOnImage:(UIImage *)newOnImage +{ + if (newOnImage != onImage) + { + [onImage release]; + onImage = [newOnImage copy]; + toggleLayer.onImage = onImage; + [toggleLayer setNeedsDisplay]; + } +} + +- (void)setOffImage:(UIImage *)newOffImage +{ + if (newOffImage != offImage) + { + [offImage release]; + offImage = [newOffImage copy]; + toggleLayer.offImage = offImage; + [toggleLayer setNeedsDisplay]; + } +} + @end diff --git a/DCRoundSwitch/DCRoundSwitchToggleLayer.h b/DCRoundSwitch/DCRoundSwitchToggleLayer.h index e56c631..904b5f0 100644 --- a/DCRoundSwitch/DCRoundSwitchToggleLayer.h +++ b/DCRoundSwitch/DCRoundSwitchToggleLayer.h @@ -17,10 +17,13 @@ @property (nonatomic, retain) UIColor *onTintColor; @property (nonatomic, retain) NSString *onString; @property (nonatomic, retain) NSString *offString; +@property (nonatomic, retain) UIImage *onImage; +@property (nonatomic, retain) UIImage *offImage; @property (nonatomic, readonly) UIFont *labelFont; @property (nonatomic) BOOL drawOnTint; @property (nonatomic) BOOL clip; - (id)initWithOnString:(NSString *)anOnString offString:(NSString *)anOffString onTintColor:(UIColor *)anOnTintColor; +- (id)initWithOnImage:(UIImage *)anOnImage offImage:(UIImage *)anOffImage onTintColor:(UIColor *)anOnTintColor; @end diff --git a/DCRoundSwitch/DCRoundSwitchToggleLayer.m b/DCRoundSwitch/DCRoundSwitchToggleLayer.m index 99e0550..564b1b0 100644 --- a/DCRoundSwitch/DCRoundSwitchToggleLayer.m +++ b/DCRoundSwitch/DCRoundSwitchToggleLayer.m @@ -12,7 +12,7 @@ #import "DCRoundSwitchToggleLayer.h" @implementation DCRoundSwitchToggleLayer -@synthesize onString, offString, onTintColor; +@synthesize onString, offString, onImage, offImage, onTintColor; @synthesize drawOnTint; @synthesize clip; @synthesize labelFont; @@ -22,6 +22,8 @@ - (void)dealloc [onString release]; [offString release]; [onTintColor release]; + [onImage release]; + [offImage release]; [super dealloc]; } @@ -33,11 +35,25 @@ - (id)initWithOnString:(NSString *)anOnString offString:(NSString *)anOffString self.onString = anOnString; self.offString = anOffString; self.onTintColor = anOnTintColor; + self.onImage = nil; + self.offImage = nil; } return self; } +- (id)initWithOnImage:(UIImage *)anOnImage offImage:(UIImage *)anOffImage onTintColor:(UIColor *)anOnTintColor +{ + if ((self = [super init])) + { + self.onImage = anOnImage; + self.offImage = anOffImage; + self.onTintColor = anOnTintColor; + } + + return self; +} + - (UIFont *)labelFont { return [UIFont boldSystemFontOfSize:ceilf(self.bounds.size.height * .6)]; @@ -79,23 +95,35 @@ - (void)drawInContext:(CGContextRef)context CGFloat textSpaceWidth = (self.bounds.size.width / 2) - (knobRadius / 2); UIGraphicsPushContext(context); - - // 'ON' state label (self.onString) - CGSize onTextSize = [self.onString sizeWithFont:self.labelFont]; - CGPoint onTextPoint = CGPointMake((textSpaceWidth - onTextSize.width) / 2.0 + knobRadius * .15, floorf((self.bounds.size.height - onTextSize.height) / 2.0) + 1.0); - [[UIColor colorWithWhite:0.45 alpha:1.0] set]; // .2 & .4 - [self.onString drawAtPoint:CGPointMake(onTextPoint.x, onTextPoint.y - 1.0) withFont:self.labelFont]; - [[UIColor whiteColor] set]; - [self.onString drawAtPoint:onTextPoint withFont:self.labelFont]; - - // 'OFF' state label (self.offString) - CGSize offTextSize = [self.offString sizeWithFont:self.labelFont]; - CGPoint offTextPoint = CGPointMake(textSpaceWidth + (textSpaceWidth - offTextSize.width) / 2.0 + knobRadius * .86, floorf((self.bounds.size.height - offTextSize.height) / 2.0) + 1.0); - [[UIColor whiteColor] set]; - [self.offString drawAtPoint:CGPointMake(offTextPoint.x, offTextPoint.y + 1.0) withFont:self.labelFont]; - [[UIColor colorWithWhite:0.52 alpha:1.0] set]; - [self.offString drawAtPoint:offTextPoint withFont:self.labelFont]; - +//withOffset:drawPercent * (boundsRect.size.width - knobWidth) +//inTrackWidth:(boundsRect.size.width - knobWidth)]; + + if (self.onImage) { + CGPoint onImagePoint = CGPointMake((textSpaceWidth - self.onImage.size.width) / 2.0 + knobRadius * .15, floorf((self.bounds.size.height - self.onImage.size.height) / 2.0) + 1.0); + [self.onImage drawAtPoint:onImagePoint]; + } else { + // 'ON' state label (self.onString) + CGSize onTextSize = [self.onString sizeWithFont:self.labelFont]; + CGPoint onTextPoint = CGPointMake((textSpaceWidth - onTextSize.width) / 2.0 + knobRadius * .15, floorf((self.bounds.size.height - onTextSize.height) / 2.0) + 1.0); + [[UIColor colorWithWhite:0.45 alpha:1.0] set]; // .2 & .4 + [self.onString drawAtPoint:CGPointMake(onTextPoint.x, onTextPoint.y - 1.0) withFont:self.labelFont]; + [[UIColor whiteColor] set]; + [self.onString drawAtPoint:onTextPoint withFont:self.labelFont]; + } + + if (self.offImage) { + CGPoint offImagePoint = CGPointMake(textSpaceWidth + (textSpaceWidth - self.onImage.size.width) / 2.0 + knobRadius * .86, floorf((self.bounds.size.height - self.onImage.size.height) / 2.0) + 1.0); + [offImage drawAtPoint:offImagePoint]; + } else { + // 'OFF' state label (self.offString) + CGSize offTextSize = [self.offString sizeWithFont:self.labelFont]; + CGPoint offTextPoint = CGPointMake(textSpaceWidth + (textSpaceWidth - offTextSize.width) / 2.0 + knobRadius * .86, floorf((self.bounds.size.height - offTextSize.height) / 2.0) + 1.0); + [[UIColor whiteColor] set]; + [self.offString drawAtPoint:CGPointMake(offTextPoint.x, offTextPoint.y + 1.0) withFont:self.labelFont]; + [[UIColor colorWithWhite:0.52 alpha:1.0] set]; + [self.offString drawAtPoint:offTextPoint withFont:self.labelFont]; + } + UIGraphicsPopContext(); }