效果图:
示例代码:
#pragma mark 添加加载进度
- (void)addLoadProgress
{
IWLoadingProgressView *loadProgress = [IWLoadingProgressView progressView];
loadProgress.frame = CGRectMake(0, 0, AdaptedWidth(100), AdaptedHeight(100));
loadProgress.progress = 0.0;
loadProgress.center = self.view.center;
[self.view addSubview:loadProgress];
}
自定义控件代码:
#import <UIKit/UIKit.h>
@interface IWLoadingProgressView : UIView
//进度值
@property (nonatomic,assign) CGFloat progress;
//清除指示器
- (void)dismiss;
//示例化对象
+ (id)progressView;
@end
#import "IWLoadingProgressView.h"
static const CGFloat WLoadingProgressViewItemMargin = 10;
@implementation IWLoadingProgressView
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor colorWithRed:240/255.0 green:240/255.0 blue:240/255.0 alpha:1.0];
self.layer.cornerRadius = 5;
self.clipsToBounds = YES;
}
return self;
}
- (void)setProgress:(CGFloat)progress
{
_progress = progress;
if (progress >= 1.0) {
[self removeFromSuperview];
}else {
[self setNeedsDisplay];
}
}
- (void)drawRect:(CGRect)rect
{
//获取上下文
CGContextRef context = UIGraphicsGetCurrentContext();
//获取中心点
CGFloat centerX = rect.size.width * 0.5;
CGFloat centerY = rect.size.height * 0.5;
//设置线宽
CGContextSetLineWidth(context, 4);
//设置目标角度
CGFloat to = M_PI * 2 * _progress;
CGFloat radius = MIN(centerX, centerY) - WLoadingProgressViewItemMargin;
//添加路径
CGContextAddArc(context, centerX, centerY, radius, 0, to, 0);
//设置填充颜色
[KGlobalBlueColor set];
CGContextStrokePath(context);
//添加路径2
CGContextAddArc(context, centerX, centerY, radius, to, M_PI * 2, 0);
[[UIColor whiteColor] set];
CGContextStrokePath(context);
//加载时显示的文字
NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
attributes[NSFontAttributeName] = [UIFont systemFontOfSize:14.0];
attributes[NSForegroundColorAttributeName] = [UIColor colorWithRed:234/255.0 green:186/255.0 blue:133/255.0 alpha:1.0];
NSInteger p = _progress * 100;
NSString *showText = [NSString stringWithFormat:@"%ld",(long)p];
showText = [showText stringByAppendingString:@"%"];
[self setCenterProgressText:showText withAttributes:attributes];
}
#pragma mark 设置加载时显示的文字
- (void)setCenterProgressText:(NSString *)text withAttributes:(NSDictionary *)attributes
{
CGFloat centerX = self.frame.size.width * 0.5;
CGFloat centerY = self.frame.size.height * 0.5;
CGSize strSize;
NSAttributedString *attrStr = nil;
if (attributes[NSFontAttributeName]) {
strSize = [text sizeWithAttributes:@{NSFontAttributeName:attributes[NSFontAttributeName]}];
attrStr = [[NSAttributedString alloc] initWithString:text attributes:attributes];
} else {
strSize = [text sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:[UIFont systemFontSize]]}];
attrStr = [[NSAttributedString alloc] initWithString:text attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:[UIFont systemFontSize]]}];
}
CGFloat strX = centerX - strSize.width * 0.5;
CGFloat strY = centerY - strSize.height * 0.5;
[attrStr drawAtPoint:CGPointMake(strX, strY)];
}
#pragma mark 清除指示器
- (void)dismiss
{
self.progress = 1.0f;
}
+ (id)progressView
{
return [[self alloc] init];
}
@end