diff --git a/SVSegmentedControl/SVSegmentedControl.h b/SVSegmentedControl/SVSegmentedControl.h index b0d9e95..a15a528 100644 --- a/SVSegmentedControl/SVSegmentedControl.h +++ b/SVSegmentedControl/SVSegmentedControl.h @@ -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]; diff --git a/SVSegmentedControl/SVSegmentedControl.m b/SVSegmentedControl/SVSegmentedControl.m index da8b527..066fe8a 100644 --- a/SVSegmentedControl/SVSegmentedControl.m +++ b/SVSegmentedControl/SVSegmentedControl.m @@ -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 @@ -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; @@ -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); @@ -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); diff --git a/SVSegmentedControl/SVSegmentedThumb.m b/SVSegmentedControl/SVSegmentedThumb.m index 10f4c3f..0843477 100644 --- a/SVSegmentedControl/SVSegmentedThumb.m +++ b/SVSegmentedControl/SVSegmentedThumb.m @@ -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]; @@ -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); @@ -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); @@ -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); @@ -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; }