如何实现画圆角

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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值