#import "PPSectorView.h"
#define PI 3.14159265358979323846
@interface PPSectorView ()
@end
@implementation PPSectorView
//计算度转弧度
static inline float radians(double degrees) {
return degrees * PI / 180;
}
-(void)drawRect:(CGRect)rect
{
if(!_fillColor || ![_fillColor isKindOfClass:[UIColor class]] || (ESectorTypeRectangle == self.sectorType))
{
return;
}
if(self.radius <= 0 || self.frame.size.width == 0 || self.frame.size.height == 0 )
{
return;
}
else if((ESectorTypeLeftUp == self.sectorType || ESectorTypeRightUp == self.sectorType || ESectorTypeLeftDown == self.sectorType || ESectorTypeRightDown == self.sectorType) && !(self.frame.size.width == self.frame.size.height && self.frame.size.width == self.radius))
{
return;
}
else if((ESectorTypeUp == self.sectorType || ESectorTypeDown == self.sectorType) && !(self.frame.size.width == 2*self.frame.size.height && self.frame.size.height == self.radius))
{
return;
}
else if((ESectorTypeLeft == self.sectorType || ESectorTypeRight == self.sectorType) && !(self.frame.size.width*2 == self.frame.size.height && self.frame.size.width == self.radius))
{
return;
}
else if((ESectorTypeCircle == self.sectorType) && !(self.frame.size.width == self.frame.size.height && self.frame.size.width == 2*self.radius))
{
return;
}
CGPoint cent=CGPointMake((self.frame.size.width)/2, (self.frame.size.height)/2);
if(ESectorTypeLeftUp == self.sectorType)
{
cent=CGPointMake((self.frame.size.width), (self.frame.size.height));
}
else if(ESectorTypeRightUp == self.sectorType)
{
cent=CGPointMake(0, (self.frame.size.height));
}
else if(ESectorTypeLeftDown == self.sectorType)
{
cent=CGPointMake((self.frame.size.width), 0);
}
else if(ESectorTypeRightDown == self.sectorType)
{
cent=CGPointMake(0, 0);
}
else if(ESectorTypeUp == self.sectorType)
{
cent=CGPointMake((self.frame.size.width)/2,self.frame.size.height);
}
else if(ESectorTypeDown == self.sectorType)
{
cent=CGPointMake((self.frame.size.width)/2,0);
}
else if(ESectorTypeLeft == self.sectorType)
{
cent=CGPointMake((self.frame.size.width), (self.frame.size.height)/2);
}
else if(ESectorTypeRight == self.sectorType)
{
cent=CGPointMake(0, (self.frame.size.height)/2);
}
else if(ESectorTypeCircle == self.sectorType)
{
cent=CGPointMake((self.frame.size.width)/2, (self.frame.size.height)/2);
}
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextClearRect(ctx, rect);
float angle_start = radians(180.0);
float angle_end = radians(270.0);
if(ESectorTypeLeftDown == self.sectorType)
{
angle_start = radians(90.0);
angle_end = radians(180.0);
}
else if(ESectorTypeRightDown == self.sectorType)
{
angle_start = radians(0.0);
angle_end = radians(90.0);
}
else if(ESectorTypeRightUp == self.sectorType)
{
angle_start = radians(270.0);
angle_end = radians(360.0);
}
else if(ESectorTypeLeft == self.sectorType)
{
angle_start = radians(90.0);
angle_end = radians(270.0);
}
else if(ESectorTypeRight == self.sectorType)
{
angle_start = radians(270.0);
angle_end = radians(90.0);
}
else if(ESectorTypeUp == self.sectorType)
{
angle_start = radians(180.0);
angle_end = radians(360.0);
}
else if(ESectorTypeDown == self.sectorType)
{
angle_start = radians(0.0);
angle_end = radians(180.0);
}
else if(ESectorTypeCircle == self.sectorType)
{
angle_start = radians(0.0);
angle_end = radians(360.0);
}
CGContextMoveToPoint(ctx, cent.x, cent.y);
CGContextSetFillColor(ctx, CGColorGetComponents( [self.fillColor CGColor]));
CGContextAddArc(ctx, cent.x, cent.y, self.radius, angle_start, angle_end, 0);
CGContextFillPath(ctx);
}
-(void)setAttributesWithIsBackgroundClear:(BOOL)isBackgroundClear radius:(CGFloat)radius fillColor:(UIColor *)fillColor sectorType:(ESectorType)sectorType
{
self.isBackgroundClear = isBackgroundClear;
self.radius = radius;
self.fillColor = fillColor;
self.sectorType = sectorType;
if(self.isBackgroundClear)
{
self.backgroundColor = [UIColor clearColor];
}
if(ESectorTypeRectangle == self.sectorType)
{
self.backgroundColor = fillColor;
}
}
@end