如何实现画圆角

简介: 如何实现画圆角
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
目录
相关文章
|
5月前
|
前端开发
Canvas绘画设计之网格线背景设置成网格源码
Canvas绘画设计之网格线背景设置成网格源码
|
5月前
|
前端开发
|
5月前
|
Web App开发 前端开发
canvas系列教程04 —— 渐变、阴影、路径、状态、Canvas对象、图形重叠模式
canvas系列教程04 —— 渐变、阴影、路径、状态、Canvas对象、图形重叠模式
458 0
设计----背景颜色透明和渐变颜色
设计----背景颜色透明和渐变颜色
|
6月前
给图片添加圆角功能,圆角透明
给图片添加圆角功能,圆角透明
29 0
|
7月前
如何实现带背景的镂空文字
如何实现带背景的镂空文字
42 1
|
7月前
按钮的image图片是非圆角,直接对UIButton设置圆角,iOS13系统没有圆角效果的问题及解决方案
按钮的image图片是非圆角,直接对UIButton设置圆角,iOS13系统没有圆角效果的问题及解决方案
50 0
|
7月前
|
前端开发
【零基础入门前端系列】—旋转、缩放、倾斜、过渡(二十三)
【零基础入门前端系列】—旋转、缩放、倾斜、过渡(二十三)
|
7月前
|
前端开发
【零基础入门前端系列】—圆角和阴影(二十一)
【零基础入门前端系列】—圆角和阴影(二十一)