iOS 加载进度之圆形加载

效果图

ezgif.com-video-to-gif.gif

使用方法

/**
 * 提示文案
 * 默认 正在加载
 */
+ (nonnull GYProgressView *)gy_showProgressWithAlertStr:(NSString *_Nullable)alertStr progressFinishedHandle:(void(^)(void))progressFinishedHandle;

/**
 * 设置进度
 */
- (void)setProgressWithPercentage:(CGFloat)percentage;

/**
 * 销毁页面
 */
- (void)gy_dismissView;

核心代码

  • 1、绘制圆
 // 黑色的背景框
    CAShapeLayer *bordLayer = [CAShapeLayer layer];
    UIBezierPath *bordPath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, 148, 116) byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(6, 6)];
    bordLayer.path = bordPath.CGPath;
    self.alertView.layer.mask = bordLayer;
    
    // 进度的底色
    UIBezierPath *bgRoundPath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(20, 20) radius:20 startAngle:0 endAngle:M_PI * 2 clockwise:YES];
    self.bgProgressLayer.path = bgRoundPath.CGPath;
    [self.bgProgressView.layer addSublayer:self.bgProgressLayer];
    
    // 进度的颜色
    UIBezierPath *roundPath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(20, 20) radius:20 startAngle:-M_PI_2 endAngle:-M_PI_2 + M_PI *2 clockwise:YES];
    self.progressLayer.path = roundPath.CGPath;
    [self.progressView.layer addSublayer:self.progressLayer];
    self.progressLayer.strokeStart = 0;
    self.progressLayer.strokeEnd = 0;

  • 2、设置进度
_progressLayer.strokeEnd = _percentage;
NSString *str = [NSString stringWithFormat:@"%.0f%%",_percentage*100]; 
_progressLabel.text = str;

注意事项

为防止 外界set 值过快, 导致 实际显示跟 值有差值,设定一个定时器定时去取值

- (void)setProgressWithPercentage:(CGFloat)percentage {
    self.percentage = percentage;
    if (self.timer == nil && !self.canRemoveTimer) {
        self.timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(updataUI) userInfo:nil repeats:YES];
    }
}

Demo地址

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Swift1> Swift和OC的区别1.1> Swift没有地址/指针的概念1.2> 泛型1.3> 类型严谨 对...
    cosWriter阅读 11,789评论 1 32
  • 【Android 动画】 动画分类补间动画(Tween动画)帧动画(Frame 动画)属性动画(Property ...
    Rtia阅读 6,508评论 1 38
  • 前言 本文只要描述了iOS中的Core Animation(核心动画:隐式动画、显示动画)、贝塞尔曲线、UIVie...
    GitHubPorter阅读 3,769评论 7 11
  • 第一章:Activity生命周期和启动模式 Activity关闭时会调用onPause()和onStop(),如果...
    loneyzhou阅读 1,101评论 0 2
  • 在自然中跳禅舞是一种美妙的体验,慢慢地转圈,周围的群山在眼前清晰,高远,空阔。当你安静,阳光也是安静散发能量的。
    墨荷沙沙阅读 330评论 0 1

友情链接更多精彩内容