IOS 圆圈加载动画(中间带有文本,可放进度值等)

效果图:


示例代码:

#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




  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值