Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Please review and take this changes #22

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions DCRoundSwitch/DCRoundSwitch.m
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,10 @@ - (void)setup
self.toggleLayer = [[[[[self class] toggleLayerClass] alloc] initWithOnString:self.onText offString:self.offText onTintColor:[UIColor colorWithRed:0.000 green:0.478 blue:0.882 alpha:1.0]] autorelease];
self.toggleLayer.drawOnTint = NO;
self.toggleLayer.clip = YES;
if( self.toggleLayer.bounds.size.height == 0 )
{
self.toggleLayer.frame = CGRectMake( 0, 0, self.bounds.size.height, self.bounds.size.height);
}
[self.layer addSublayer:self.toggleLayer];
[self.toggleLayer setNeedsDisplay];

Expand Down Expand Up @@ -240,7 +244,7 @@ - (void)tapped:(UITapGestureRecognizer *)gesture
if (self.ignoreTap) return;

if (gesture.state == UIGestureRecognizerStateEnded)
[self setOn:!self.on animated:YES];
[self setOn:!self.on animated:YES ignoreControlEvents:NO];
}

- (void)toggleDragged:(UIPanGestureRecognizer *)gesture
Expand Down Expand Up @@ -285,7 +289,7 @@ - (void)toggleDragged:(UIPanGestureRecognizer *)gesture
{
// flip the switch to on or off depending on which half it ends at
CGFloat toggleCenter = CGRectGetMidX(self.toggleLayer.frame);
[self setOn:(toggleCenter > CGRectGetMidX(self.bounds)) animated:YES];
[self setOn:(toggleCenter > CGRectGetMidX(self.bounds)) animated:YES ignoreControlEvents:NO];
}

// send off the appropriate actions (not fully tested yet)
Expand Down Expand Up @@ -329,14 +333,19 @@ - (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer;

#pragma mark Setters/Getters

-(void)setEnabled:(BOOL)aenabled {
[super setEnabled:aenabled];
[self.toggleLayer setEnabled:aenabled];
}

- (void)setOn:(BOOL)newOn
{
[self setOn:newOn animated:NO];
[self setOn:newOn animated:NO ignoreControlEvents:YES];
}

- (void)setOn:(BOOL)newOn animated:(BOOL)animated
{
[self setOn:newOn animated:animated ignoreControlEvents:NO];
[self setOn:newOn animated:animated ignoreControlEvents:YES];
}

- (void)setOn:(BOOL)newOn animated:(BOOL)animated ignoreControlEvents:(BOOL)ignoreControlEvents
Expand Down Expand Up @@ -417,7 +426,7 @@ - (void)setOnTintColor:(UIColor *)anOnTintColor
}
}

- (void)layoutSubviews;
- (void)layoutSubviews
{
CGFloat knobRadius = self.bounds.size.height + 2.0;
self.knobLayer.frame = CGRectMake(0, 0, knobRadius, knobRadius);
Expand Down Expand Up @@ -445,10 +454,11 @@ - (void)layoutSubviews;

- (void)setOnText:(NSString *)newOnText
{
if (newOnText != onText)
if ( ![newOnText isEqualToString:onText] )
{
[onText release];
onText = [newOnText copy];
[self sizeToFit];
self.toggleLayer.onString = onText;
[self.toggleLayer setNeedsDisplay];
}
Expand All @@ -460,6 +470,7 @@ - (void)setOffText:(NSString *)newOffText
{
[offText release];
offText = [newOffText copy];
[self sizeToFit];
self.toggleLayer.offString = offText;
[self.toggleLayer setNeedsDisplay];
}
Expand Down
1 change: 1 addition & 0 deletions DCRoundSwitch/DCRoundSwitchToggleLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
@property (nonatomic, readonly) UIFont *labelFont;
@property (nonatomic) BOOL drawOnTint;
@property (nonatomic) BOOL clip;
@property (nonatomic) BOOL enabled;

- (id)initWithOnString:(NSString *)anOnString offString:(NSString *)anOffString onTintColor:(UIColor *)anOnTintColor;

Expand Down
7 changes: 7 additions & 0 deletions DCRoundSwitch/DCRoundSwitchToggleLayer.m
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ - (UIFont *)labelFont
return [UIFont boldSystemFontOfSize:ceilf(self.bounds.size.height * .6)];
}

-(void)setEnabled:(BOOL)aenabled {
if(aenabled)
self.opacity = 1.0;
else
self.opacity = 0.65;
}

- (void)drawInContext:(CGContextRef)context
{
CGFloat knobRadius = self.bounds.size.height - 2.0;
Expand Down