国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > iOS > 正文

iOS實現(xiàn)步驟進度條功能實例代碼

2020-07-26 02:22:47
字體:
供稿:網(wǎng)友

前言

在開發(fā)中,我們經(jīng)常在很多場景下需要用到進度條,比如文件的下載,或者文件的上傳等。 本文主要給大家介紹的是一個步驟進度條效果,步驟進度條效果參考

iOS UIKit 框架中并沒有提供類似的控件,我們可以使用 UIProgressView、UIView、UILabel 組合實現(xiàn)步驟進度條效果。

  • UIProgressView――實現(xiàn)水平的進度條效果;
  • UIView――把UIView裁剪成圓形,實現(xiàn)索引節(jié)點效果;
  • UILabel――每個節(jié)點下面的提示文字。

源碼

將步驟進度條封裝成一個 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"http:// 步驟條主題色#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; // 進度條 [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;  // 進度條 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)前進度索引,更新圓形圖片、文本顏色、當(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è)置進度條 [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];}

效果:


因為 UIProgressView 實現(xiàn)的水平進度條高度值默認為1,設(shè)置frame是無效的。可以通過仿射變換的方式增加它的高度。

第三方框架

  • GitHub: ISTimeline ⭐️900
  • GitHub: TimelineTableViewCell ⭐️800

參考:

  • iOS 自定義步驟進度條
  • XFStepProgress

總結(jié):

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,如果有疑問大家可以留言交流,謝謝大家對武林網(wǎng)的支持。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 孟津县| 凤凰县| 都江堰市| 清远市| 营山县| 三门峡市| 贵南县| 铜山县| 莱西市| 长治县| 广汉市| 陕西省| 高碑店市| 闸北区| 康平县| 调兵山市| 洛阳市| 子长县| 常德市| 孝义市| 顺平县| 清远市| 宁南县| 广平县| 巴马| 厦门市| 荆州市| 陇西县| 神木县| 青海省| 抚松县| 延吉市| 沁阳市| 桓台县| 陇西县| 武陟县| 长乐市| 徐州市| 伊金霍洛旗| 河池市| 疏附县|