iOS-自定义的画圆或弧的UIView
CustomViewOfCircle.h
#import <UIKit/UIKit.h>
@interface CustomViewOfCircle : UIView
{
//是否自定义属性:myDot
BOOL isDefinedMyDot;
//是否自定义属性:myRadius
BOOL isDefinedMyRadius;
//是否自定义属性:myAngle
BOOL isDefinedMyAngle;
//是否自定义属性:MyLineWidth
BOOL isDefinedMyLineWidth;
//是否自定义属性:MyClockWise
BOOL isDefinedMyClockWise;
}
//圆圈或弧线的边线的颜色,默认为黑色
@property (nonatomic,strong) UIColor* myStrokeColor;
//线的宽度,默认为1.0
@property (nonatomic,assign) CGFloat myLineWidth;
//圆圈或弧线的圆周中心点坐标,默认为当前视图的中心点,即(self.frame.size.width/2,self.frame.size.height/2)
@property (nonatomic,assign) CGPoint myDot;
//圆圈或弧线的半径,默认采用当前视图尺寸(self.frame.size)来计算半径
//计算规则:
//如果 self.frame.size.width > self.frame.size.height ,
//那么 半径=self.frame.size.height/2
//否则 半径=self.frame.size.width/2
//也就是取高、宽中值小的那个的1/2作为半径
@property (nonatomic,assign) CGFloat myRadius;
//圆圈或弧线的范围,用弧度来计算,圆一周总弧度为2*PI(即360度角)。默认值为(0,2*PI)
//angle.x :弧线起点的弧度
//angle.y :弧线终点的弧度
//
//
// ^ 1.5*PI弧度
// |
// |
// |
// |
// |
// 1*PI弧度 |
// -------------------------------->0弧度(2*PI弧度)
// |
// |
// |
// |
// |
// |0.5*PI弧度
//
//
//
//
@property (nonatomic,assign) CGPoint myAngle;
//画弧线方向,为0表示顺势正,为1表示逆时针,默认值为0.
//方向不同,画出的弧线也会不同,例如:
//假设 参数myAngle定义为(0 , 0.5*PI)
//如果 参数myClockWise=0,即将从上图中0弧度开始,沿顺时针方向画弧线到0.5*PI弧度位置,即画了一条90度角的弧线
//如果 参数myClockWise=1,即将从上图中0弧度开始,沿逆时针方向经过1.5*PI弧度、1*PI弧度,然后一直画弧线到0.5*PI弧度位置,相当于画了一条270度角的弧线
@property (nonatomic,assign) int myClockWise;
@end
CustomViewOfCircle.m
#import "CustomViewOfCircle.h"
#define PI M_PI //圆周率常量
#define default_start_angle 0.0 //默认起始弧度
#define default_end_angle 2*PI //默认终点弧度
#define default_line_width 1.0 //默认线宽
@implementation CustomViewOfCircle
#pragma mark --------------->系统方法区<---------------
-(instancetype)init{
self=[super init];
if(self){
}
return self;
}
-(void)drawRect:(CGRect)rect{
//圆心坐标点
if (!isDefinedMyDot) {
_myDot.x =self.frame.size.width/2;
_myDot.y =self.frame.size.height/2;
}
//圆半径
if (!isDefinedMyRadius) {
_myRadius=(self.frame.size.width>self.frame.size.height)?self.frame.size.height/2:self.frame.size.width/2;
}
//弧度
if (!isDefinedMyAngle) {
_myAngle.x=default_start_angle;
_myAngle.y=default_end_angle;
}
//线宽
if (!isDefinedMyLineWidth) {
_myLineWidth=default_line_width;
}
//方向
if (!isDefinedMyClockWise) {
_myClockWise=0;
}
CGContextRef context = UIGraphicsGetCurrentContext();
//画弧线
CGContextSetStrokeColorWithColor(context, _myStrokeColor.CGColor);
CGContextSetLineWidth(context, _myLineWidth);//线的宽度
CGContextAddArc(context, _myDot.x, _myDot.y, _myRadius, _myAngle.x, _myAngle.y, _myClockWise);
CGContextDrawPath(context, kCGPathStroke); //绘制路径
}
#pragma mark --------------->自定义属性方法区<---------------
-(void)setMyCircleDot : (CGPoint) myCircleDot{
_myDot.x=myCircleDot.x;
_myDot.y=myCircleDot.y;
isDefinedMyDot=YES;
}
-(void)setMyRadius:(CGFloat)myRadius{
_myRadius=myRadius;
isDefinedMyRadius=YES;
}
-(void)setMyAngle:(CGPoint)myAngle{
_myAngle.x=myAngle.x;
_myAngle.y=myAngle.y;
isDefinedMyAngle=YES;
}
-(void)setMyLineWidth:(CGFloat)myLineWidth{
_myLineWidth=myLineWidth;
isDefinedMyLineWidth=YES;
}
-(void)setMyClockWise:(int)myClockWise{
_myClockWise=myClockWise;
isDefinedMyClockWise=YES;
}
@end
具体调用方法:
#import "ViewController894.h"
#import "CustomViewOfCircle.h"
@interface ViewController894 ()
@end
@implementation ViewController894
- (void)viewDidLoad {
[super viewDidLoad];
[self _drawCircle];
}
- (void)_drawCircle {
//define CustomViewOfCircle
CustomViewOfCircle* cvCircle = [[CustomViewOfCircle alloc] init];
cvCircle.frame=CGRectMake(50, 100, 200, 200);
cvCircle.backgroundColor=[UIColor whiteColor];
//设置弧线颜色
[cvCircle setNewStrokeColor:[UIColor redColor]];
//设置半径
[cvCircle setNewRadius:cvCircle.frame.size.width/2-10];
//设置线宽
[cvCircle setNewLineWidth:2.0];
//设置弧线起点、终点弧度
[cvCircle setNewAngle:CGPointMake(0.0*M_PI,1.5*M_PI)];
//设置画弧线方向
[cvCircle setNewClockWise:1];
//add subview
[self.view addSubview:cvCircle];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end
起始弧度 0,终点弧度 0.5*PI ,逆时针方向
起始弧度 0,终点弧度 0.5*PI ,顺时针方向
标签:自定义,弧线,frame,iOS,弧度,画圆,PI,self,size From: https://blog.51cto.com/u_16160131/6473926