iOS實(shí)現(xiàn)步驟進(jìn)度條功能實(shí)例代碼
前言
在開(kāi)發(fā)中,我們經(jīng)常在很多場(chǎng)景下需要用到進(jìn)度條,比如文件的下載,或者文件的上傳等。 本文主要給大家介紹的是一個(gè)步驟進(jìn)度條效果,步驟進(jìn)度條效果參考
iOS UIKit 框架中并沒(méi)有提供類似的控件,我們可以使用 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; } // 布局更新頁(yè)面元素 - (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是無(wú)效的??梢酝ㄟ^(guò)仿射變換的方式增加它的高度。
第三方框架
- GitHub: ISTimeline ⭐️900
- GitHub: TimelineTableViewCell ⭐️800
參考:
總結(jié):
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
- 使用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)帶動(dòng)畫(huà)的環(huán)形進(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)封裝一個(gè)獲取通訊錄的工具類詳解
這篇文章主要給大家介紹了關(guān)于iOS如何實(shí)現(xiàn)封裝一個(gè)獲取通訊錄的工具類的相關(guān)資料,這是自己平時(shí)封裝的一個(gè)工具類,使用非常方便,文中給出了詳細(xì)的示例代碼,需要的朋友們可以參考借鑒,下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-10-10iOS11 下載之?dāng)帱c(diǎn)續(xù)傳的bug的解決方法
本篇文章主要介紹了iOS11 下載之?dāng)帱c(diǎn)續(xù)傳的bug的解決方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-11-11iOS block循環(huán)引用詳解及常見(jiàn)誤區(qū)
這篇文章主要介紹了iOS block循環(huán)引用詳解和應(yīng)用,常見(jiàn)誤區(qū)詳解,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2021-08-08實(shí)例講解如何在iOS應(yīng)用開(kāi)發(fā)中使用設(shè)計(jì)模式中的代理模式
這篇文章主要介紹了實(shí)例講解如何在iOS應(yīng)用開(kāi)發(fā)中使用設(shè)計(jì)模式中的代理模式,示例為傳統(tǒng)的Objective-C語(yǔ)言代碼,需要的朋友可以參考下2016-03-03通過(guò)UIKit坐標(biāo)系來(lái)全面掌握iOS中的UIScrollView組件
iOS開(kāi)發(fā)套件中的UIScrollView組件十分強(qiáng)大,不僅是滾動(dòng),縮放操作也能夠控制自如,其核心當(dāng)然是坐標(biāo)軸上的控制,下面就通過(guò)UIKit坐標(biāo)系來(lái)全面掌握iOS中的UIScrollView組件2016-05-05iOS開(kāi)發(fā)-調(diào)用系統(tǒng)相機(jī)和相冊(cè)獲取照片示例
這篇文章主要介紹了iOS開(kāi)發(fā)-調(diào)用系統(tǒng)相機(jī)和相冊(cè)獲取照片示例的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-02-02深入了解iOS開(kāi)發(fā)中UIWindow的相關(guān)使用
這篇文章主要介紹了深入了解iOS開(kāi)發(fā)中UIWindow的相關(guān)使用,以及iOS8以后產(chǎn)生的相關(guān)變化,需要的朋友可以參考下2015-10-10Flutter之TabBarView組件項(xiàng)目實(shí)戰(zhàn)示例
這篇文章主要為大家介紹了Flutter之TabBarView組件項(xiàng)目實(shí)戰(zhàn)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10