iOS實(shí)現(xiàn)步驟進(jìn)度條功能實(shí)例代碼
前言
在開發(fā)中,我們經(jīng)常在很多場景下需要用到進(jìn)度條,比如文件的下載,或者文件的上傳等。 本文主要給大家介紹的是一個(gè)步驟進(jìn)度條效果,步驟進(jìn)度條效果參考

iOS UIKit 框架中并沒有提供類似的控件,我們可以使用 UIProgressView、UIView、UILabel 組合實(shí)現(xiàn)步驟進(jìn)度條效果。
- UIProgressView——實(shí)現(xiàn)水平的進(jìn)度條效果;
- UIView——把UIView裁剪成圓形,實(shí)現(xiàn)索引節(jié)點(diǎn)效果;
- UILabel——每個(gè)節(jié)點(diǎn)下面的提示文字。
源碼
將步驟進(jìn)度條封裝成一個(gè) HQLStepView 類,它是 UIView 的子類。
HQLStepView.h 文件
#import <UIKit/UIKit.h> @interface HQLStepView : UIView // 指定初始化方法 - (instancetype)initWithFrame:(CGRect)frame titlesArray:(NSArray *)titlesArray stepIndex:(NSUInteger)stepIndex; // 設(shè)置當(dāng)前步驟 - (void)setStepIndex:(NSUInteger)stepIndex animation:(BOOL)animation; @end
HQLStepView.m 文件
#import "HQLStepView.h"
// 步驟條主題色
#define TINT_COLOR [UIColor colorWithRed:35/255.f green:135/255.f blue:255/255.f alpha:1]
@interface HQLStepView ()
@property (nonatomic, copy) NSArray *titlesArray;
@property (nonatomic, assign) NSUInteger stepIndex;
@property (nonatomic, strong) UIProgressView *progressView;
@property (nonatomic, strong) NSMutableArray *circleViewArray;
@property (nonatomic, strong) NSMutableArray *titleLabelArray;
@property (nonatomic, strong) UILabel *indicatorLabel;
@end
@implementation HQLStepView
#pragma mark - Init
- (instancetype)initWithFrame:(CGRect)frame titlesArray:(NSArray *)titlesArray stepIndex:(NSUInteger)stepIndex {
self = [super initWithFrame:frame];
if (self) {
_titlesArray = [titlesArray copy];
_stepIndex = stepIndex;
// 進(jìn)度條
[self addSubview:self.progressView];
for (NSString *title in _titlesArray) {
// 圓圈
UIView *circle = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 13, 13)];
circle.backgroundColor = [UIColor lightGrayColor];
circle.layer.cornerRadius = 13.0f / 2;
[self addSubview:circle];
[self.circleViewArray addObject:circle];
// 標(biāo)題
UILabel *label = [[UILabel alloc] init];
label.text = title;
label.font = [UIFont systemFontOfSize:14];
label.textAlignment = NSTextAlignmentCenter;
[self addSubview:label];
[self.titleLabelArray addObject:label];
}
// 當(dāng)前索引數(shù)字
[self addSubview:self.indicatorLabel];
}
return self;
}
// 布局更新頁面元素
- (void)layoutSubviews {
NSInteger perWidth = self.frame.size.width / self.titlesArray.count;
// 進(jìn)度條
self.progressView.frame = CGRectMake(0, 0, self.frame.size.width - perWidth, 1);
self.progressView.center = CGPointMake(self.frame.size.width / 2, self.frame.size.height / 4);
CGFloat startX = self.progressView.frame.origin.x;
for (int i = 0; i < self.titlesArray.count; i++) {
// 圓圈
UIView *cycle = self.circleViewArray[i];
if (cycle) {
cycle.center = CGPointMake(i * perWidth + startX, self.progressView.center.y);
}
// 標(biāo)題
UILabel *label = self.titleLabelArray[i];
if (label) {
label.frame = CGRectMake(perWidth * i, self.frame.size.height / 2, self.frame.size.width / self.titlesArray.count, self.frame.size.height / 2 );
}
}
self.stepIndex = self.stepIndex;
}
#pragma mark - Custom Accessors
- (UIProgressView *)progressView {
if (!_progressView) {
_progressView = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleDefault];
_progressView.progressTintColor = TINT_COLOR;
_progressView.progress = self.stepIndex / ((self.titlesArray.count - 1) * 1.0);
}
return _progressView;
}
- (UILabel *)indicatorLabel {
if (!_indicatorLabel) {
_indicatorLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 23, 23)];
_indicatorLabel.textColor = TINT_COLOR;
_indicatorLabel.textAlignment = NSTextAlignmentCenter;
_indicatorLabel.backgroundColor = [UIColor whiteColor];
_indicatorLabel.layer.cornerRadius = 23.0f / 2;
_indicatorLabel.layer.borderColor = [TINT_COLOR CGColor];
_indicatorLabel.layer.borderWidth = 1;
_indicatorLabel.layer.masksToBounds = YES;
}
return _indicatorLabel;
}
- (NSMutableArray *)circleViewArray {
if (!_circleViewArray) {
_circleViewArray = [[NSMutableArray alloc] initWithCapacity:self.titlesArray.count];
}
return _circleViewArray;
}
- (NSMutableArray *)titleLabelArray {
if (!_titleLabelArray) {
_titleLabelArray = [[NSMutableArray alloc] initWithCapacity:self.titlesArray.count];
}
return _titleLabelArray;
}
// 設(shè)置當(dāng)前進(jìn)度索引,更新圓形圖片、文本顏色、當(dāng)前索引數(shù)字
- (void)setStepIndex:(NSUInteger)stepIndex {
for (int i = 0; i < self.titlesArray.count; i++) {
UIView *cycle = self.circleViewArray[i];
UILabel *label = self.titleLabelArray[i];
if (stepIndex >= i) {
cycle.backgroundColor = TINT_COLOR;
label.textColor = TINT_COLOR;
} else {
cycle.backgroundColor = [UIColor lightGrayColor];
label.textColor = [UIColor lightGrayColor];
}
}
}
#pragma mark - Public
- (void)setStepIndex:(NSUInteger)stepIndex animation:(BOOL)animation {
if (stepIndex < self.titlesArray.count) {
// 更新顏色
self.stepIndex = stepIndex;
// 設(shè)置進(jìn)度條
[self.progressView setProgress:stepIndex / ((self.titlesArray.count - 1) * 1.0) animated:animation];
// 設(shè)置當(dāng)前索引數(shù)字
self.indicatorLabel.text = [NSString stringWithFormat:@"%lu", stepIndex + 1];
self.indicatorLabel.center = ((UIView *)[self.circleViewArray objectAtIndex:stepIndex]).center;
}
}
@end
接口調(diào)用:
- (void)viewDidLoad {
[super viewDidLoad];
// 初始化
_hqlStepView = [[HQLStepView alloc] initWithFrame:CGRectMake(0, 200, self.view.frame.size.width, 60) titlesArray:@[@"第一步", @"第二步", @"第三步"] stepIndex:0];
[self.view addSubview:_hqlStepView];
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
// 設(shè)置當(dāng)前步驟,步驟索引=數(shù)組索引
[_hqlStepView setStepIndex:0 animation:YES];
}
效果:

因?yàn)?UIProgressView 實(shí)現(xiàn)的水平進(jìn)度條高度值默認(rèn)為1,設(shè)置frame是無效的??梢酝ㄟ^仿射變換的方式增加它的高度。
第三方框架
- GitHub: ISTimeline ⭐️900
- GitHub: TimelineTableViewCell ⭐️800
參考:
總結(jié):
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
- 使用axios實(shí)現(xiàn)上傳圖片進(jìn)度條功能
- iOS中利用CoreAnimation實(shí)現(xiàn)一個(gè)時(shí)間的進(jìn)度條效果
- ios開發(fā)加載webview顯示進(jìn)度條實(shí)例
- iOS 進(jìn)度條、加載、安裝動(dòng)畫的簡單實(shí)現(xiàn)
- Android仿IOS ViewPager滑動(dòng)進(jìn)度條
- iOS實(shí)現(xiàn)帶動(dòng)畫的環(huán)形進(jìn)度條
- iOS快速實(shí)現(xiàn)環(huán)形漸變進(jìn)度條
- iOS中使用NSProgress類來創(chuàng)建UI進(jìn)度條的方法詳解
- IOS實(shí)現(xiàn)簡單的進(jìn)度條功能
- iOS中WKWebView仿微信加載進(jìn)度條
相關(guān)文章
Flutter?ScrollController滾動(dòng)監(jiān)聽及控制示例詳解
這篇文章主要為大家介紹了Flutter?ScrollController滾動(dòng)監(jiān)聽及控制示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11
iOS UITextField最大字符數(shù)和字節(jié)數(shù)的限制詳解
在開發(fā)中我們經(jīng)常遇到這樣的需求:在UITextField或者UITextView中限制用戶可以輸入的最大字符數(shù)。但在UITextView , UITextfield 中有很多坑,網(wǎng)上的方法也很多。但是并不是很全面吧,這里全面進(jìn)行了總結(jié),有需要的朋友們可以參考借鑒,下面跟著小編一起來學(xué)習(xí)學(xué)習(xí)吧。2016-11-11
iOS 圖片上傳使用base64或者二進(jìn)制流上傳頭像功能
這篇文章主要介紹了iOS 圖片上傳使用base64或者二進(jìn)制流上傳頭像功能,需要的朋友可以參考下2017-09-09
iOS開發(fā)中關(guān)鍵字const/static/extern、UIKIT_EXTERN的區(qū)別和用法
這篇文章主要介紹了iOS 關(guān)鍵字const/static/extern、UIKIT_EXTERN區(qū)別和用法,需要的朋友可以參考下2017-12-12
IOS實(shí)現(xiàn)視頻動(dòng)畫效果的啟動(dòng)圖
這篇文章實(shí)現(xiàn)的是一個(gè)關(guān)于啟動(dòng)頁或者引導(dǎo)頁的視頻動(dòng)畫效果的實(shí)現(xiàn)過程,對(duì)于大家開發(fā)APP具有一定的參考借鑒價(jià)值,有需要的可以來看看。2016-09-09
iOS基于UIScrollView實(shí)現(xiàn)滑動(dòng)引導(dǎo)頁
這篇文章主要為大家詳細(xì)介紹了iOS基于UIScrollView實(shí)現(xiàn)滑動(dòng)引導(dǎo)頁的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01

