(1)創建自定義view。
(2)導入QuartzCore框架
(3)在h文件,定義一個屬性,用於接收進度值,最大值是1,最小值是0:
#import <UIKit/UIKit.h>
@interface ZCView : UIView
@property(nonatomic,assign)CGFloat progress;
@end
(4)m文件的代碼如下:
#import "ZCView.h"
#import <QuartzCore/QuartzCore.h>
@interface ZCView ()
@property(nonatomic,weak)UILabel *progressLabel;
@end
@implementation ZCView
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if(self)
{
CGFloat proX = 0;
CGFloat proW = frame.size.width;
CGFloat proH = 20;
CGFloat proY = (frame.size.height-proH)/2;
UILabel *progressLabel = [[UILabel alloc]initWithFrame:CGRectMake(proX, proY, proW, proH)];
[progressLabel setTextAlignment:NSTextAlignmentCenter];
self.progressLabel = progressLabel;
[self addSubview:progressLabel];
}
return self;
}
- (void)drawRect:(CGRect)rect {
//線寬
CGFloat lineWidth = 5;
//半徑
CGFloat radius = (rect.size.width-lineWidth*4)/2;
//圓心
CGPoint center = CGPointMake(rect.size.width/2, rect.size.width/2);
//結束弧度
CGFloat endAngle = -M_PI_2+(_progress*M_PI*2);
//路徑
UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:-M_PI_2 endAngle:endAngle clockwise:YES];
[path setLineWidth:lineWidth];
[path stroke];
}
- (void)setProgress:(CGFloat)progress
{
_progress = progress;
[self.progressLabel setText:[NSString stringWithFormat:@"%.2f%%",progress*100]];
[self setNeedsDisplay];
}
@end
傳入progress的值就能刷新進度了: