Skip to content

Commit

Permalink
Add a cornerRadius property.
Browse files Browse the repository at this point in the history
  • Loading branch information
samvermette committed Jan 12, 2012
1 parent b04d388 commit e6961ea
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 21 deletions.
1 change: 1 addition & 0 deletions SVSegmentedControl/SVSegmentedControl.h
Expand Up @@ -27,6 +27,7 @@
@property (nonatomic, readwrite) CGFloat height; // default is 32.0
@property (nonatomic, readwrite) UIEdgeInsets thumbEdgeInset; // default is UIEdgeInsetsMake(2, 2, 3, 2)
@property (nonatomic, readwrite) UIEdgeInsets titleEdgeInsets; // default is UIEdgeInsetsMake(0, 10, 0, 10)
@property (nonatomic, readwrite) CGFloat cornerRadius; // default is 4.0

@property (nonatomic, retain) UIFont *font; // default is [UIFont boldSystemFontOfSize:15]
@property (nonatomic, retain) UIColor *textColor; // default is [UIColor grayColor];
Expand Down
26 changes: 13 additions & 13 deletions SVSegmentedControl/SVSegmentedControl.m
Expand Up @@ -54,7 +54,7 @@ - (void)toggle;
@implementation SVSegmentedControl

@synthesize selectedSegmentChangedHandler, selectedIndex, animateToInitialSelection;
@synthesize backgroundImage, font, textColor, textShadowColor, textShadowOffset, segmentPadding, titleEdgeInsets, height, crossFadeLabelsOnDrag;
@synthesize cornerRadius, backgroundImage, font, textColor, textShadowColor, textShadowOffset, segmentPadding, titleEdgeInsets, height, crossFadeLabelsOnDrag;
@synthesize titlesArray, thumb, thumbRects, snapToIndex, trackingThumb, moved, activated, halfSize, dragOffset, segmentWidth, thumbHeight;

// deprecated
Expand Down Expand Up @@ -102,6 +102,7 @@ - (id)initWithSectionTitles:(NSArray*)array {
self.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 10);
self.thumbEdgeInset = UIEdgeInsetsMake(2, 2, 3, 2);
self.height = 32.0;
self.cornerRadius = 4.0;

self.selectedIndex = 0;
self.thumb.segmentedControl = self;
Expand Down Expand Up @@ -171,9 +172,8 @@ - (void)drawRect:(CGRect)rect {

else {
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();
float cornerRadius = 4;

CGPathRef roundedRect = [UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:cornerRadius].CGPath;
CGPathRef roundedRect = [UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:self.cornerRadius].CGPath;
CGContextAddPath(context, roundedRect);
CGContextClip(context);

Expand All @@ -183,21 +183,21 @@ - (void)drawRect:(CGRect)rect {
CGGradientRelease(gradient);

UIBezierPath *bottomGloss = [UIBezierPath bezierPath];
[bottomGloss moveToPoint:CGPointMake(0, rect.size.height-cornerRadius-0.5)];
[bottomGloss addArcWithCenter:CGPointMake(cornerRadius, rect.size.height-cornerRadius-0.5) radius:cornerRadius startAngle:M_PI endAngle:M_PI/2 clockwise:NO];
[bottomGloss addLineToPoint:CGPointMake(rect.size.width-cornerRadius, rect.size.height-0.5)];
[bottomGloss addArcWithCenter:CGPointMake(rect.size.width-cornerRadius, rect.size.height-cornerRadius-0.5) radius:cornerRadius startAngle:M_PI/2 endAngle:0 clockwise:NO];
[bottomGloss moveToPoint:CGPointMake(0, rect.size.height-self.cornerRadius-0.5)];
[bottomGloss addArcWithCenter:CGPointMake(self.cornerRadius, rect.size.height-self.cornerRadius-0.5) radius:self.cornerRadius startAngle:M_PI endAngle:M_PI/2 clockwise:NO];
[bottomGloss addLineToPoint:CGPointMake(rect.size.width-self.cornerRadius, rect.size.height-0.5)];
[bottomGloss addArcWithCenter:CGPointMake(rect.size.width-self.cornerRadius, rect.size.height-self.cornerRadius-0.5) radius:self.cornerRadius startAngle:M_PI/2 endAngle:0 clockwise:NO];
CGContextAddPath(context, bottomGloss.CGPath);
CGContextSetStrokeColorWithColor(context, [UIColor colorWithWhite:1 alpha:0.1].CGColor);
CGContextStrokePath(context);

UIBezierPath *topShadow = [UIBezierPath bezierPath];
[topShadow moveToPoint:CGPointMake(-0.3, rect.size.height-cornerRadius-0.5)];
[topShadow addLineToPoint:CGPointMake(0, cornerRadius+0.5)];
[topShadow addArcWithCenter:CGPointMake(cornerRadius, cornerRadius+0.5) radius:cornerRadius startAngle:M_PI endAngle:3*M_PI/2 clockwise:YES];
[topShadow addLineToPoint:CGPointMake(rect.size.width-cornerRadius, 0.5)];
[topShadow addArcWithCenter:CGPointMake(rect.size.width-cornerRadius, cornerRadius+0.5) radius:cornerRadius startAngle:3*M_PI/2 endAngle:0 clockwise:YES];
[topShadow addLineToPoint:CGPointMake(rect.size.width+0.3, rect.size.height-cornerRadius-0.5)];
[topShadow moveToPoint:CGPointMake(-0.3, rect.size.height-self.cornerRadius-0.5)];
[topShadow addLineToPoint:CGPointMake(0, self.cornerRadius+0.5)];
[topShadow addArcWithCenter:CGPointMake(self.cornerRadius, self.cornerRadius+0.5) radius:self.cornerRadius startAngle:M_PI endAngle:3*M_PI/2 clockwise:YES];
[topShadow addLineToPoint:CGPointMake(rect.size.width-self.cornerRadius, 0.5)];
[topShadow addArcWithCenter:CGPointMake(rect.size.width-self.cornerRadius, self.cornerRadius+0.5) radius:self.cornerRadius startAngle:3*M_PI/2 endAngle:0 clockwise:YES];
[topShadow addLineToPoint:CGPointMake(rect.size.width+0.3, rect.size.height-self.cornerRadius-0.5)];
CGContextAddPath(context, topShadow.CGPath);
CGContextSetShadowWithColor(UIGraphicsGetCurrentContext(), CGSizeMake(0, 1), 0, [UIColor colorWithWhite:0 alpha:0.2].CGColor);
CGContextSetStrokeColorWithColor(context, [UIColor colorWithWhite:0 alpha:0.9].CGColor);
Expand Down
20 changes: 12 additions & 8 deletions SVSegmentedControl/SVSegmentedThumb.m
Expand Up @@ -51,11 +51,6 @@ - (id)initWithFrame:(CGRect)frame {
if (self) {
self.userInteractionEnabled = NO;
self.backgroundColor = [UIColor clearColor];
self.layer.shadowOffset = CGSizeMake(0, 0);
self.layer.shadowRadius = 1;
self.layer.shadowColor = [UIColor blackColor].CGColor;
self.layer.shadowOpacity = 1;
self.layer.shouldRasterize = YES;

self.textColor = [UIColor whiteColor];
self.shadowColor = [UIColor blackColor];
Expand Down Expand Up @@ -107,12 +102,14 @@ - (void)drawRect:(CGRect)rect {

else {

CGFloat cornerRadius = self.segmentedControl.cornerRadius;

CGContextRef context = UIGraphicsGetCurrentContext();
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();

// STROKE GRADIENT

CGPathRef strokeRect = [UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:2].CGPath;
CGPathRef strokeRect = [UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:cornerRadius-2].CGPath;
CGContextAddPath(context, strokeRect);
CGContextClip(context);

Expand All @@ -135,7 +132,7 @@ - (void)drawRect:(CGRect)rect {

// FILL GRADIENT

CGPathRef fillRect = [UIBezierPath bezierPathWithRoundedRect:CGRectInset(rect, 1, 1) cornerRadius:1].CGPath;
CGPathRef fillRect = [UIBezierPath bezierPathWithRoundedRect:CGRectInset(rect, 1, 1) cornerRadius:cornerRadius-1].CGPath;
CGContextAddPath(context, fillRect);
CGContextClip(context);

Expand All @@ -148,7 +145,7 @@ - (void)drawRect:(CGRect)rect {
fillComponents[0]-=0.1;
fillComponents[2]-=0.1;
}

CGGradientRef fillGradient = CGGradientCreateWithColorComponents(colorSpace, fillComponents, NULL, 2);
CGContextDrawLinearGradient(context, fillGradient, CGPointMake(0,0), CGPointMake(0,CGRectGetHeight(rect)), 0);
CGGradientRelease(fillGradient);
Expand Down Expand Up @@ -218,6 +215,13 @@ - (void)setFrame:(CGRect)newFrame {
posY--;

self.label.frame = self.secondLabel.frame = CGRectMake(0, posY, newFrame.size.width, self.font.pointSize);

self.layer.shadowOffset = CGSizeMake(0, 0);
self.layer.shadowRadius = 1;
self.layer.shadowColor = [UIColor blackColor].CGColor;
self.layer.shadowOpacity = 1;
self.layer.shadowPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds cornerRadius:self.segmentedControl.cornerRadius-2].CGPath;
self.layer.shouldRasterize = YES;
}


Expand Down

0 comments on commit e6961ea

Please sign in to comment.