Skip to content

Instantly share code, notes, and snippets.

@yuraksisa
Created May 30, 2014 08:08
Show Gist options
  • Save yuraksisa/0fa4af16f8aec309cf2a to your computer and use it in GitHub Desktop.
Save yuraksisa/0fa4af16f8aec309cf2a to your computer and use it in GitHub Desktop.
Create a control for IOS that draws a Sector
#import <UIKit/UIKit.h>
@interface SectorControl : UIControl
@property UIColor* color;
@property UIColor* borderColor;
- (id) init:(float)angle withRadius:(float) radius andColor:(UIColor*) color andBorderColor:(UIColor*) borderColor;
@end
#import "SectorControl.h"
@implementation SectorControl
float alfa;
float r;
float x;
float y;
float r_y;
float x2;
CGRect frame;
- (id) init:(float)angle withRadius:(float) radius andColor:(UIColor*) color andBorderColor:(UIColor *)borderColor
{
alfa = angle;
r = radius;
x = cos((M_PI/2) - (alfa/2)) * r;
y = sqrtf(r*r - x*x);
r_y = r-y;
x2 = 2*x;
frame = CGRectMake(0,0,r,x2);
self.color = color;
self.borderColor = borderColor;
return [self initWithFrame:frame];
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
self.backgroundColor = [UIColor clearColor];
}
return self;
}
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
[super drawRect:rect];
// Drawing code
UIBezierPath *path = [UIBezierPath bezierPath];
path.lineWidth = 4.0f;
[path moveToPoint:CGPointMake(r,x)];
[path addLineToPoint:CGPointMake(r_y,0)];
[path addArcWithCenter:CGPointMake(r,x) radius:r startAngle: 3*M_PI/2 - ((M_PI - alfa) / 2)endAngle: M_PI/2 + ((M_PI - alfa)/2) clockwise:FALSE];
[path closePath];
[self.color setFill];
[self.borderColor setStroke];
[path fill];
[path stroke];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment