環(huán)形下載進(jìn)度條 簡(jiǎn)單使用

先直接上效果圖(可用于文件下載進(jìn)度簡(jiǎn)單顯示):

代碼部分也很簡(jiǎn)單涉枫,寫成一個(gè)工具類 .h文件屬性部分均可自定義

.h 文件
@interface FTCycleView : UIView

@property (nonatomic, strong) UILabel *progresslabel;     /**< 顯示進(jìn)度的Label */
@property (nonatomic, strong) UIColor *fillColor;         /**< 填充顏色 */
@property (nonatomic, strong) UIColor *strokeColor;       /**< 邊框路徑顏色 */
@property (nonatomic, assign) CGFloat lineWidth;          /**< 線的寬度 */

/** 繪制環(huán)形進(jìn)度條 
    progress:進(jìn)度值 0~1.0
 */
- (void)drawCycleProgress:(CGFloat)progress;

@end

.m 文件
@interface FTCycleView () {
    CGFloat preProgress;       /**< 先前進(jìn)度 */
}
@implementation FTCycleView

#pragma mark - get
- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame: frame]) {
       /* 設(shè)置默認(rèn)顏色和環(huán)形進(jìn)度條寬度 */
        preProgress = 0;
        _fillColor = [UIColor  clearColor];
        _strokeColor = [UIColor greenColor];
        _lineWidth = 10;
    }
    return self;
}

 /** 顯示進(jìn)度的Label */
- (UILabel *)progresslabel {
    if (_progresslabel == nil) {
        _progresslabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width*0.8, self.frame.size.height*0.4)];
        _progresslabel.textAlignment = NSTextAlignmentCenter;
        _progresslabel.center = CGPointMake(self.frame.size.width/2, self.frame.size.height/2);
        [_progresslabel setFont:[UIFont systemFontOfSize:10.0*(self.frame.size.width/35.0)]];
        [self addSubview:_progresslabel];
    }
    return _progresslabel;
}

/** 繪制環(huán)形進(jìn)度條
 progress:進(jìn)度值 0~1.0
 */
- (void)drawCycleProgress:(CGFloat)progress {
    self.progresslabel.text = [NSString stringWithFormat:@"%.0f%%",progress*100];
    
    // 獲取環(huán)形路徑 (畫一個(gè)圓 填充色透明 設(shè)置線框?qū)挾葹?0墨坚,獲得環(huán)形)
    CAShapeLayer *progressLayer = [CAShapeLayer layer];
    progressLayer.frame = self.bounds;
    progressLayer.fillColor = self.fillColor.CGColor;              // 填充顏色
    progressLayer.strokeColor = self.strokeColor.CGColor;          // 邊框路徑顏色
    //    _progressLayer.opacity = 1;                              // 背景顏色的透明度
    progressLayer.lineCap = kCALineCapRound;                       // 線的邊緣是圓的
    progressLayer.lineWidth = self.lineWidth;                      // 線的寬度
    
    CGPoint center = CGPointMake(self.frame.size.width/2, self.frame.size.width/2);
    CGFloat radius = self.frame.size.width/2;
    CGFloat startA = - M_PI_2 + M_PI*2*preProgress;         // 起始點(diǎn)
    CGFloat endA = - M_PI_2 + M_PI*2*progress;              // 終點(diǎn) 
    preProgress = progress;
    
    // 繪制
    UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:center radius:radius
                                                    startAngle:startA endAngle:endA clockwise:YES];
    progressLayer.path = path.CGPath;
    [self.layer addSublayer: progressLayer];
    
    // 動(dòng)畫效果
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
    animation.fromValue = @(0.0);
    animation.toValue = @(1.0);
    animation.duration = 0.1;
    [progressLayer addAnimation:animation forKey:nil];
}

在其他ViewController中調(diào)用

#import "ViewController.h"
#import "FTCycleView.h"

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    [self cycleProgressTest];
}

/** 
     模擬下載顯示 環(huán)形進(jìn)度條 
     實(shí)際應(yīng)用中只需調(diào)用下面方法 傳入當(dāng)前下載進(jìn)度值即可
     [cycleView drawCycleProgress:(當(dāng)前下載進(jìn)度值)];  
*/
- (void)cycleProgressTest {
    
    FTCycleView *cycleView = [[FTCycleView alloc] initWithFrame:CGRectMake(100, 100, 200, 200)];
    [self.view addSubview:cycleView];
    
    __block NSInteger progress = 0;
    [NSTimer scheduledTimerWithTimeInterval:0.5 repeats:YES block:^(NSTimer * _Nonnull timer) {
        progress = progress + 10;
        if (progress <= 100) {
            // 實(shí)際應(yīng)用中只需調(diào)用下面方法 傳入當(dāng)前下載進(jìn)度值即可
            [cycleView drawCycleProgress:progress/100.0];  
            
        } else {
            [timer invalidate];
        }
    }];
}

@end

Demo下載

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末寥枝,一起剝皮案震驚了整個(gè)濱河市宝当,隨后出現(xiàn)的幾起案子识窿,更是在濱河造成了極大的恐慌佩谣,老刑警劉巖悉稠,帶你破解...
    沈念sama閱讀 217,542評(píng)論 6 504
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異赊抖,居然都是意外死亡统倒,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,822評(píng)論 3 394
  • 文/潘曉璐 我一進(jìn)店門氛雪,熙熙樓的掌柜王于貴愁眉苦臉地迎上來房匆,“玉大人,你說我怎么就攤上這事报亩≡『瑁” “怎么了?”我有些...
    開封第一講書人閱讀 163,912評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵弦追,是天一觀的道長岳链。 經(jīng)常有香客問我,道長骗卜,這世上最難降的妖魔是什么宠页? 我笑而不...
    開封第一講書人閱讀 58,449評(píng)論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮寇仓,結(jié)果婚禮上举户,老公的妹妹穿的比我還像新娘。我一直安慰自己遍烦,他們只是感情好俭嘁,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,500評(píng)論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著服猪,像睡著了一般供填。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上罢猪,一...
    開封第一講書人閱讀 51,370評(píng)論 1 302
  • 那天近她,我揣著相機(jī)與錄音,去河邊找鬼膳帕。 笑死粘捎,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播攒磨,決...
    沈念sama閱讀 40,193評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼泳桦,長吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了娩缰?” 一聲冷哼從身側(cè)響起灸撰,我...
    開封第一講書人閱讀 39,074評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎拼坎,沒想到半個(gè)月后浮毯,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,505評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡演痒,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,722評(píng)論 3 335
  • 正文 我和宋清朗相戀三年亲轨,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了趋惨。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片鸟顺。...
    茶點(diǎn)故事閱讀 39,841評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖器虾,靈堂內(nèi)的尸體忽然破棺而出讯嫂,到底是詐尸還是另有隱情,我是刑警寧澤兆沙,帶...
    沈念sama閱讀 35,569評(píng)論 5 345
  • 正文 年R本政府宣布欧芽,位于F島的核電站,受9級(jí)特大地震影響葛圃,放射性物質(zhì)發(fā)生泄漏千扔。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,168評(píng)論 3 328
  • 文/蒙蒙 一库正、第九天 我趴在偏房一處隱蔽的房頂上張望曲楚。 院中可真熱鬧,春花似錦褥符、人聲如沸龙誊。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,783評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽趟大。三九已至,卻和暖如春铣焊,著一層夾襖步出監(jiān)牢的瞬間逊朽,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,918評(píng)論 1 269
  • 我被黑心中介騙來泰國打工曲伊, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留叽讳,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 47,962評(píng)論 2 370
  • 正文 我出身青樓,卻偏偏與公主長得像绽榛,于是被迫代替她去往敵國和親湿酸。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,781評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 172,116評(píng)論 25 707
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫灭美、插件推溃、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 12,103評(píng)論 4 62
  • Oracle安裝完后犁苏,其中有一個(gè)缺省的數(shù)據(jù)庫硬萍,除了這個(gè)缺省的數(shù)據(jù)庫外,我們還可以創(chuàng)建自己的數(shù)據(jù)庫围详。 對(duì)于初學(xué)者來說...
    lichengjin閱讀 746評(píng)論 0 0
  • 里格島:湖水環(huán)抱的摩挲古村 里格島是瀘沽湖西北湖畔一個(gè)美麗的半島朴乖,背靠格姆女神山,三面環(huán)水助赞,坐擁無敵湖景买羞。這里有最...
    攜程攻略社區(qū)閱讀 529評(píng)論 0 0
  • 前天老師教了一首童詩,要家長錄下來孩子背詩的過程雹食,并發(fā)到家長群里畜普,當(dāng)天晚上我和老公一句一句教樂樂,教的我們...
    維佳覺醒系統(tǒng)閱讀 787評(píng)論 2 1