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地址

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

  • Swift1> Swift和OC的區(qū)別1.1> Swift沒有地址/指針的概念1.2> 泛型1.3> 類型嚴謹 對...
    cosWriter閱讀 11,656評論 1 32
  • 【Android 動畫】 動畫分類補間動畫(Tween動畫)幀動畫(Frame 動畫)屬性動畫(Property ...
    Rtia閱讀 6,386評論 1 38
  • 前言 本文只要描述了iOS中的Core Animation(核心動畫:隱式動畫、顯示動畫)、貝塞爾曲線、UIVie...
    GitHubPorter閱讀 3,741評論 7 11
  • 第一章:Activity生命周期和啟動模式 Activity關(guān)閉時會調(diào)用onPause()和onStop(),如果...
    loneyzhou閱讀 1,072評論 0 2
  • 在自然中跳禪舞是一種美妙的體驗,慢慢地轉(zhuǎn)圈,周圍的群山在眼前清晰,高遠,空闊。當你安靜,陽光也是安靜散發(fā)能量的。
    墨荷沙沙閱讀 320評論 0 1

友情鏈接更多精彩內(nèi)容