#import <UIKit/UIKit.h>
typedef enum : NSUInteger {
LEFT_RIGHT_TOP_BOTTOM, //四个角
TOP_LEFT_RIGHT, //上两个角
BOTTOM_LEFT_RIGHT, //下两个角
} RADUIS_TYPE; //角位置
@interface RectRaduisView : UIView
//要绘制的圆角数
@property (nonatomic,assign)CGFloat d_readius;
//绘制角位置的方法
@property (nonatomic,assign)RADUIS_TYPE raduisType;
@end
#import "RectRaduisView.h"
@implementation RectRaduisView{
CAShapeLayer *_shapLayer;
UIBezierPath *_bezierPath;
}
-(instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
_d_readius = 0.0f;
_raduisType = LEFT_RIGHT_TOP_BOTTOM
_shapLayer = [CAShapeLayer layer];
_bezierPath = [UIBezierPath bezierPath];
_shapLayer.path = _bezierPath.CGPath;
[self.layer addSublayer:_shapLayer];
}
return self;
}
-(void)layoutSubviews
{
[super layoutSubviews];
[_bezierPath removeAllPoints];
CGFloat width = CGRectGetWidth(self.bounds);
CGFloat height = CGRectGetHeight(self.bounds);
CGPoint point0 = CGPointMake(0, 0);
CGPoint point1 = CGPointMake(self.d_readius, 0);
CGPoint point2 = CGPointMake(width - self.d_readius, 0);
CGPoint center1 =CGPointMake(width - self.d_readius, self.d_readius);
//右上角
CGPoint point3 = CGPointMake(width, 0);
CGPoint point4 = CGPointMake(width,height - self.d_readius);
CGPoint center2 =CGPointMake(width - self.d_readius, height - self.d_readius);
//右下角
CGPoint point5 = CGPointMake(width,height);
CGPoint point6 = CGPointMake(self.d_readius,height);
CGPoint center3 =CGPointMake(self.d_readius, height - self.d_readius);
//左下角
CGPoint point7 = CGPointMake(0,height);
CGPoint point8 = CGPointMake(0,self.d_readius);
CGPoint center4 =CGPointMake(self.d_readius, self.d_readius);
if (self.raduisType==LEFT_RIGHT_TOP_BOTTOM) {
//四个角
[_bezierPath moveToPoint:point1];
[_bezierPath addLineToPoint:point2];
//第一个圆角 右上
[_bezierPath addArcWithCenter:center1 radius:self.d_readius startAngle:-M_PI_2 endAngle:0 clockwise:YES];
[_bezierPath addLineToPoint:point4];
//右下
[_bezierPath addArcWithCenter:center2 radius:self.d_readius startAngle:0 endAngle:M_PI_2 clockwise:YES];
[_bezierPath addLineToPoint:point6];
//左下
[_bezierPath addArcWithCenter:center3 radius:self.d_readius startAngle:M_PI_2 endAngle:M_PI clockwise:YES];
[_bezierPath addLineToPoint:point8];
//左下
[_bezierPath addArcWithCenter:center4 radius:self.d_readius startAngle:M_PI endAngle:M_PI*3/2.0f clockwise:YES];
}else if (self.raduisType==TOP_LEFT_RIGHT){
//上 两个
[_bezierPath moveToPoint:point1];
[_bezierPath addLineToPoint:point2];
//第一个圆角 右上
[_bezierPath addArcWithCenter:center1 radius:self.d_readius startAngle:-M_PI_2 endAngle:0 clockwise:YES];
[_bezierPath addLineToPoint:point4];
//右下
[_bezierPath addLineToPoint:point5];
[_bezierPath addLineToPoint:point6];
//左下
[_bezierPath addLineToPoint:point7];
[_bezierPath addLineToPoint:point8];
//左下
[_bezierPath addArcWithCenter:center4 radius:self.d_readius startAngle:M_PI endAngle:M_PI*3/2.0f clockwise:YES];
}else if (self.raduisType==BOTTOM_LEFT_RIGHT){
//下两个
[_bezierPath moveToPoint:point0];
[_bezierPath addLineToPoint:point1];
[_bezierPath addLineToPoint:point2];
[_bezierPath addLineToPoint:point3];
[_bezierPath addLineToPoint:point4];
//右下
[_bezierPath addArcWithCenter:center2 radius:self.d_readius startAngle:0 endAngle:M_PI_2 clockwise:YES];
[_bezierPath addLineToPoint:point6];
//左下
[_bezierPath addArcWithCenter:center3 radius:self.d_readius startAngle:M_PI_2 endAngle:M_PI clockwise:YES];
[_bezierPath addLineToPoint:point8];
//左上
[_bezierPath addLineToPoint:point0];
}
_shapLayer.path = _bezierPath.CGPath;
_shapLayer.strokeColor = [UIColor clearColor].CGColor;
_shapLayer.fillColor =self.backgroundColor.CGColor;
self.layer.mask = _shapLayer;
}
-(void)setRaduisType:(RADUIS_TYPE)raduisType
{
_raduisType = raduisType;
[self layoutSubviews];
}
@end
网友评论