千家信息网

iOS如何实现步骤进度条功能

发表于:2026-11-02 作者:千家信息网编辑
千家信息网最后更新 2026年11月02日,这篇文章主要介绍了iOS如何实现步骤进度条功能,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。源码将步骤进度条封装成一个 HQLStep
千家信息网最后更新 2026年11月02日iOS如何实现步骤进度条功能

这篇文章主要介绍了iOS如何实现步骤进度条功能,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。

源码

将步骤进度条封装成一个 HQLStepView 类,它是 UIView 的子类。

HQLStepView.h 文件

#import @interface HQLStepView : UIView// 指定初始化方法- (instancetype)initWithFrame:(CGRect)frame titlesArray:(NSArray *)titlesArray stepIndex:(NSUInteger)stepIndex;// 设置当前步骤- (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; // 进度条 [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]; // 标题 UILabel *label = [[UILabel alloc] init]; label.text = title; label.font = [UIFont systemFontOfSize:14]; label.textAlignment = NSTextAlignmentCenter; [self addSubview:label]; [self.titleLabelArray addObject:label]; } // 当前索引数字 [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); } // 标题 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;}// 设置当前进度索引,更新圆形图片、文本颜色、当前索引数字- (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; // 设置进度条 [self.progressView setProgress:stepIndex / ((self.titlesArray.count - 1) * 1.0) animated:animation]; // 设置当前索引数字 self.indicatorLabel.text = [NSString stringWithFormat:@"%lu", stepIndex + 1]; self.indicatorLabel.center = ((UIView *)[self.circleViewArray objectAtIndex:stepIndex]).center; }}@end

接口调用:

- (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]; // 设置当前步骤,步骤索引=数组索引 [_hqlStepView setStepIndex:0 animation:YES];}

感谢你能够认真阅读完这篇文章,希望小编分享的"iOS如何实现步骤进度条功能"这篇文章对大家有帮助,同时也希望大家多多支持,关注行业资讯频道,更多相关知识等着你来学习!

步骤 进度 索引 篇文章 数字 更新 功能 圆圈 文件 标题 颜色 主题 价值 元素 兴趣 同时 图片 圆形 子类 布局 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 互联网科技公司经营范围 科技互联网产品 互联网科技公司华为 没上市的科技互联网公司 极限挑战互联网科技 睿民互联网科技 互联网科技视频素材 一五八零互联网科技 华脉科技工业互联网产品 重庆威客帮互联网科技 上海宁丞科技互联网 智腾互联网科技有限公司 互联网科技创新的路径 卓翼科技产业互联网合作 2021互联网科技公司排名 产业互联网科技服务公司 半导体是互联网高科技吗 易企秀互联网科技是做什么的 上海同邻互联网科技有限公司规模 北京军民互联网科技公司 互联网科技金融 互联网科技公司名称 世界互联网科技公司市值排名 成都互联网科技公司工作好不好 恒生互联网科技业指数调整周期 为客找房互联网科技公司怎么样 福建互联网展示科技馆设计 世界互联网科技成果奖 深圳互联网科技园地址 安徽九吞吴互联网科技有限公司
0