如何实现画圆角

简介: 如何实现画圆角
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
目录
相关文章
|
6天前
给图片添加圆角功能,圆角透明
给图片添加圆角功能,圆角透明
7 0
|
2月前
如何实现带背景的镂空文字
如何实现带背景的镂空文字
20 1
|
2月前
按钮的image图片是非圆角,直接对UIButton设置圆角,iOS13系统没有圆角效果的问题及解决方案
按钮的image图片是非圆角,直接对UIButton设置圆角,iOS13系统没有圆角效果的问题及解决方案
22 0
|
2月前
|
前端开发
前端原生 CSS 跑马灯效果,无限轮播(横竖版本,带渐变遮罩,简单实用)
前端原生 CSS 跑马灯效果,无限轮播(横竖版本,带渐变遮罩,简单实用)
31 0
|
8月前
|
前端开发
【前端切图】CSS文字渐变和背景渐变
【前端切图】CSS文字渐变和背景渐变
45 0
|
11月前
|
前端开发
前端 SVG 与 Canvas 框架案例 (画线、矩形、箭头、文字 ....)
前端 SVG 与 Canvas 框架案例 (画线、矩形、箭头、文字 ....)
115 0
CSS3文本居中显示、圆形圆角绘制、立体阴影效果设置实例演示
CSS3文本居中显示、圆形圆角绘制、立体阴影效果设置实例演示
106 0
|
前端开发 IDE 开发工具
「趣学前端」为什么有的页面背景颜色是渐变的
用技术实现梦想,用梦想打开创意之门。为了给不懂技术的朋友讲解日常开发中怎么实现网页的效果,我准备出一个系列。
142 1
|
iOS开发
封装图片设置为圆形
封装图片设置为圆形
100 0
封装图片设置为圆形