typedef NS_ENUM(NSUInteger, ESectorType)
{
ESectorTypeLeftUp = 0, //左上角90度扇形
ESectorTypeRightUp = 1, //右上角90度扇形
ESectorTypeLeftDown = 2, //左下角90度扇行
ESectorTypeRightDown = 3, //右下角90度扇形
ESectorTypeLeft = 4, //左半圆
ESectorTypeRight = 5, //右半圆
ESectorTypeUp = 6, //上半圆
ESectorTypeDown = 7, //下半圆
ESectorTypeCircle = 8, //全圆
ESectorTypeRectangle = 9, //矩形
};
@interface PPSectorView : BGBaseView
@property (nonatomic, strong) UIColor *fillColor;
@property (nonatomic, assign) BOOL isBackgroundClear;
@property (nonatomic, assign) CGFloat radius;
@property (nonatomic, assign) ESectorType sectorType;
//背景只能设置透明与非透明,设置非透明背景颜色实际是黑色
-(void)setAttributesWithIsBackgroundClear:(BOOL)isBackgroundClear radius:(CGFloat)radius fillColor:(UIColor *)fillColor sectorType:(ESectorType)sectorType;
@end
#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