iOS實(shí)現(xiàn)帶動(dòng)畫(huà)的環(huán)形進(jìn)度條
本篇寫(xiě)的是實(shí)現(xiàn)環(huán)形進(jìn)度條,并帶動(dòng)畫(huà)效果,要實(shí)現(xiàn)這些,僅能通過(guò)自己畫(huà)一個(gè)
方法直接看代碼
為了方便多次調(diào)用,用繼承UIView的方式
.m文件
#import <UIKit/UIKit.h> @interface LoopProgressView : UIView @property (nonatomic, assign) CGFloat progress; @end
.h文件
NSTimer的調(diào)用并非精確,可以自行百度
這里因?yàn)槊?.01s啟動(dòng)一次定時(shí)器,所以要同步進(jìn)度條和數(shù)字,就將self.progress賦值給動(dòng)畫(huà)的duration屬性就可以了,duration為動(dòng)畫(huà)時(shí)間。
在使用時(shí)我發(fā)現(xiàn)如果在tableviewcell中添加了這個(gè)環(huán)形進(jìn)度條時(shí)有個(gè)缺點(diǎn),就是定時(shí)器原本用的是系統(tǒng)的runloop,導(dǎo)致數(shù)據(jù)顯示滯后,所以現(xiàn)更新為子線程里添加定時(shí)器,子線程的定時(shí)器必須添加[[NSRunLoop currentRunLoop] run];才可啟動(dòng)定時(shí)器,因?yàn)樽泳€程的runloop里是不帶nstimer的,要手動(dòng)添加運(yùn)行。
#import "LoopProgressView.h"
#import <QuartzCore/QuartzCore.h>
#define ViewWidth self.frame.size.width //環(huán)形進(jìn)度條的視圖寬度
#define ProgressWidth 2.5 //環(huán)形進(jìn)度條的圓環(huán)寬度
#define Radius ViewWidth/2-ProgressWidth //環(huán)形進(jìn)度條的半徑
@interface LoopProgressView()
{
CAShapeLayer *arcLayer;
UILabel *label;
NSTimer *progressTimer;
}
@property (nonatomic,assign)CGFloat i;
@end
@implementation LoopProgressView
-(id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor clearColor];
}
return self;
}
-(void)drawRect:(CGRect)rect
{
_i=0;
CGContextRef progressContext = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(progressContext, ProgressWidth);
CGContextSetRGBStrokeColor(progressContext, 209.0/255.0, 209.0/255.0, 209.0/255.0, 1);
CGFloat xCenter = rect.size.width * 0.5;
CGFloat yCenter = rect.size.height * 0.5;
//繪制環(huán)形進(jìn)度條底框
CGContextAddArc(progressContext, xCenter, yCenter, Radius, 0, 2*M_PI, 0);
CGContextDrawPath(progressContext, kCGPathStroke);
// //繪制環(huán)形進(jìn)度環(huán)
CGFloat to = self.progress * M_PI * 2; // - M_PI * 0.5為改變初始位置
// 進(jìn)度數(shù)字字號(hào),可自己根據(jù)自己需要,從視圖大小去適配字體字號(hào)
int fontNum = ViewWidth/6;
49 label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0,Radius+10, ViewWidth/6)];
label.center = CGPointMake(xCenter, yCenter);
label.textAlignment = NSTextAlignmentCenter;
label.font = [UIFont boldSystemFontOfSize:fontNum];
label.text = @"0%";
[self addSubview:label];
UIBezierPath *path=[UIBezierPath bezierPath];
[path addArcWithCenter:CGPointMake(xCenter,yCenter) radius:Radius startAngle:0 endAngle:to clockwise:YES];
arcLayer=[CAShapeLayer layer];
arcLayer.path=path.CGPath;//46,169,230
arcLayer.fillColor = [UIColor clearColor].CGColor;
arcLayer.strokeColor=[UIColor colorWithRed:227.0/255.0 green:91.0/255.0 blue:90.0/255.0 alpha:0.7].CGColor;
arcLayer.lineWidth=ProgressWidth;
arcLayer.backgroundColor = [UIColor blueColor].CGColor;
[self.layer addSublayer:arcLayer];
dispatch_async(dispatch_get_global_queue(0, 0), ^{
[self drawLineAnimation:arcLayer];
});
if (self.progress > 1) {
NSLog(@"傳入數(shù)值范圍為 0-1");
self.progress = 1;
}else if (self.progress < 0){
NSLog(@"傳入數(shù)值范圍為 0-1");
self.progress = 0;
return;
}
if (self.progress > 0) {
NSThread *thread = [[NSThread alloc]initWithTarget:self selector:@selector(newThread) object:nil];
[thread start];
}
}
-(void)newThread
{
@autoreleasepool {
progressTimer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(timeLabel) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] run];
}
}
//NSTimer不會(huì)精準(zhǔn)調(diào)用
-(void)timeLabel
{
_i += 0.01;
label.text = [NSString stringWithFormat:@"%.0f%%",_i*100];
if (_i >= self.progress) {
[progressTimer invalidate];
progressTimer = nil;
}
}
//定義動(dòng)畫(huà)過(guò)程
-(void)drawLineAnimation:(CALayer*)layer
{
CABasicAnimation *bas=[CABasicAnimation animationWithKeyPath:@"strokeEnd"];
bas.duration=self.progress;//動(dòng)畫(huà)時(shí)間
bas.delegate=self;
bas.fromValue=[NSNumber numberWithInteger:0];
bas.toValue=[NSNumber numberWithInteger:1];
[layer addAnimation:bas forKey:@"key"];
}
@end
完成后在要調(diào)用的控制器里,僅需幾段代碼:傳進(jìn)的參數(shù):為0-1
LoopProgressView *custom = [[LoopProgressView alloc]initWithFrame:CGRectMake(50, 100, 100, 100)]; custom.progress = 0.44; [self.view addSubview:custom];
實(shí)現(xiàn):
已經(jīng)實(shí)現(xiàn)進(jìn)度條和數(shù)字的同步:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- iOS實(shí)現(xiàn)步驟進(jìn)度條功能實(shí)例代碼
- 使用axios實(shí)現(xiàn)上傳圖片進(jìn)度條功能
- iOS中利用CoreAnimation實(shí)現(xiàn)一個(gè)時(shí)間的進(jìn)度條效果
- ios開(kāi)發(fā)加載webview顯示進(jìn)度條實(shí)例
- iOS 進(jìn)度條、加載、安裝動(dòng)畫(huà)的簡(jiǎn)單實(shí)現(xiàn)
- Android仿IOS ViewPager滑動(dòng)進(jìn)度條
- iOS快速實(shí)現(xiàn)環(huán)形漸變進(jìn)度條
- iOS中使用NSProgress類來(lái)創(chuàng)建UI進(jìn)度條的方法詳解
- IOS實(shí)現(xiàn)簡(jiǎn)單的進(jìn)度條功能
- iOS中WKWebView仿微信加載進(jìn)度條
相關(guān)文章
IOS實(shí)現(xiàn)選擇城市后跳轉(zhuǎn)Tabbar效果
這篇文章主要為大家詳細(xì)介紹了IOS實(shí)現(xiàn)選擇城市后跳轉(zhuǎn)Tabbar效果的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-07-07
iOS中的表單按鈕選項(xiàng)UIActionSheet常用方法整理
UIActionSheet經(jīng)常被用來(lái)制作各種彈出的選項(xiàng),這里我們就來(lái)看一下iOS中的表單按鈕選項(xiàng)UIActionSheet常用方法整理,需要的朋友可以參考下2016-06-06
iOS開(kāi)發(fā)中常見(jiàn)的解析XML的類庫(kù)以及簡(jiǎn)要安裝方法
這篇文章主要介紹了iOS開(kāi)發(fā)中常見(jiàn)的解析XML的類庫(kù)以及簡(jiǎn)要安裝方法,簡(jiǎn)要地說(shuō)明了在Xcode下的一些特點(diǎn)以供對(duì)比,需要的朋友可以參考下2015-10-10
iOS之?dāng)?shù)據(jù)解析之XML解析詳解
本篇文章主要介紹了iOS之?dāng)?shù)據(jù)解析之XML解析詳解,XML解析常見(jiàn)的兩種方式:DOM解析和SAX解析,有興趣的可以了解一下。2016-12-12

