0.效果图
1.新建一个自定义视图,继承自 UIView,代码如下:
.h 文件
#import <UIKit/UIKit.h>
#define WINSIZE [UIScreen mainScreen].bounds.size
@interface DJBezierPathPercent : UIView
/**
占比
*/
@property (nonatomic, assign) CGFloat percent;
@end
.m 文件
#import "DJBezierPathPercent.h"
#define progressCornerRadius WINSIZE.width * 0.164
@interface DJBezierPathPercent()
@property (nonatomic, strong) UILabel *percentLab;
@end
@implementation DJBezierPathPercent
- (instancetype)initWithFrame:(CGRect)frame {
if (self == [super initWithFrame:frame]) {
self.backgroundColor = [UIColor whiteColor];
int padding = 15;
UIView *borderView = [UIView new];
CGFloat W = progressCornerRadius * 2 + padding;
CGFloat H = W;
CGFloat X = (CGRectGetWidth(frame) - W) / 2;
CGFloat Y = (CGRectGetHeight(frame) - H) / 2;
borderView.frame = CGRectMake(X, Y, W, H);
borderView.layer.cornerRadius = (progressCornerRadius*2 + padding)/2;
borderView.layer.borderWidth = 0.5;
borderView.layer.borderColor = [UIColor lightGrayColor].CGColor;
[self addSubview:borderView];
_percentLab = [[UILabel alloc] init];
_percentLab.frame = CGRectMake(X, Y + H/2 - 30/2, W, 30);
_percentLab.textAlignment = NSTextAlignmentCenter;
_percentLab.font = [UIFont systemFontOfSize:15.f];
[self addSubview:_percentLab];
}
return self;
}
- (void)setPercent:(CGFloat)percent {
_percent = percent;
_percentLab.text = [NSString stringWithFormat:@"%.f%%",percent];
}
- (void)drawRect:(CGRect)rect {
[self drawPercent:self.percent];
}
- (void)drawPercent:(CGFloat)percent {
percent = percent / 100.f;
UIBezierPath *path = [[UIBezierPath alloc]init];
path.lineWidth = 5;
[[UIColor cyanColor] set];
[path addArcWithCenter:self.center radius:progressCornerRadius startAngle:- M_PI_2 endAngle:(2 * M_PI) * percent - M_PI_2 clockwise:YES];
[path stroke];
}
@end
控制器内调用
DJBezierPathPercent *bezier = [[DJBezierPathPercent alloc] initWithFrame:self.view.bounds];
bezier.percent = arc4random()%100;
[self.view addSubview:bezier];