1、在DrawCircle.h文件中
提供了接口,在使用的时候,可以设定圆心、半径、角度、圆环的宽度、圆环的背景底色、圆环的进度条颜色,当然后面三个有自定义的值。
//
// DrawCircle.h
// Demo-draw
//
// Created by yyt on 16/5/10.
// Copyright © 2016年 yyt. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface DrawCircle : UIView
@property(nonatomic,assign) CGPoint centerPoint;
@property(nonatomic,assign) CGFloat radius;
@property(nonatomic,assign) CGFloat angleValue; //圆环进度占有的角度,0~360
@property(nonatomic,assign) CGFloat lineWidth;
@property(nonatomic,strong) UIColor *bgLineColor;
@property(nonatomic,strong) UIColor *lineColor;
@end
2、在DrawCircle.m文件中
//
// DrawCircle.m
// Demo-draw
//
// Created by yyt on 16/5/10.
// Copyright © 2016年 yyt. All rights reserved.
//
#import "DrawCircle.h"
@implementation DrawCircle
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
self.backgroundColor = [UIColor whiteColor];
self.lineWidth = 10;
self.bgLineColor = [UIColor lightGrayColor];
self.lineColor = [UIColor orangeColor];
return self;
}
- (void)drawRect:(CGRect)rect {
CGContextRef bgContextRef = UIGraphicsGetCurrentContext();
CGContextAddArc(bgContextRef, _centerPoint.x, _centerPoint.y, _radius, 0, 10, 0);
CGContextSetLineWidth(bgContextRef, _lineWidth);
[_bgLineColor setStroke];
CGContextStrokePath(bgContextRef);
CGContextRef contextRef = UIGraphicsGetCurrentContext();
CGContextAddArc(contextRef, _centerPoint.x, _centerPoint.y, _radius, M_PI/2, M_PI/2+_angleValue/180*M_PI, 0);
CGContextSetLineWidth(contextRef, _lineWidth);
[_lineColor setStroke];
CGContextStrokePath(contextRef);
}
@end
3、在需要使用圆环进度条的地方ViewController.m文件中
//
// ViewController.m
// Demo-draw
//
// Created by yyt on 16/5/10.
// Copyright © 2016年 yyt. All rights reserved.
//
#import "ViewController.h"
#import "DrawCircle.h"
@interface ViewController ()
@property(nonatomic,strong) DrawCircle *view2;
@end
@implementation ViewController
static int hehe = 30;
- (void)viewDidLoad {
[super viewDidLoad];
DrawCircle *view2 = [[DrawCircle alloc] initWithFrame:CGRectMake(100, 200, 100, 100)];
self.view2 = view2;
view2.centerPoint = CGPointMake(50, 50);
view2.radius = 30;
view2.angleValue = hehe;
view2.lineWidth = 20;
view2.lineColor = [UIColor orangeColor];
[self.view addSubview:view2];
//进度+
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(100, 100, 100, 30);
button.backgroundColor = [UIColor blueColor];
[button addTarget:self action:@selector(hehe) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
//进度-
UIButton *button2 = [UIButton buttonWithType:UIButtonTypeSystem];
button2.frame = CGRectMake(100, 150, 100, 30);
button2.backgroundColor = [UIColor redColor];
[button2 addTarget:self action:@selector(hehe2) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button2];
}
- (void)hehe {
hehe += 30;
self.view2.angleValue = hehe;
[self.view2 setNeedsDisplay];
}
- (void)hehe2 {
hehe -= 30;
self.view2.angleValue = hehe;
[self.view2 setNeedsDisplay];
}
@end