iOS 实现步骤进度条-程序员宅基地

640?wx_fmt=gif

640?wx_fmt=jpeg

Linux编程 点击右侧关注,免费入门到精通! 640?wx_fmt=jpeg



作者丨独木舟的木
https://www.jianshu.com/p/fb471ca68a1b


640?wx_fmt=gif步骤进度条效果参考


640?wx_fmt=other


iOS UIKit 框架中并没有提供类似的控件,我们可以使用 UIProgressView、UIView、UILabel 组合实现步骤进度条效果。


  • UIProgressView——实现水平的进度条效果;

  • UIView——把UIView裁剪成圆形,实现索引节点效果;

  • UILabel——每个节点下面的提示文字。

640?wx_fmt=gif源码


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


HQLStepView.h 文件


#import <UIKit/UIKit.h>

@interface HQLStepView : UIView

// 指定初始化方法
- (instancetype)initWithFrame:(CGRect)frame titlesArray:(NSArray *)titlesArray stepIndex:(NSUInteger)stepIndex;

// 设置当前步骤
- (void)setStepIndex:(NSUInteger)stepIndex animation:(BOOL)animation;

@end


640?wx_fmt=gifHQLStepView.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 (nonatomiccopyNSArray *titlesArray;
@property (nonatomicassignNSUInteger stepIndex;

@property (nonatomicstrongUIProgressView *progressView;
@property (nonatomicstrongNSMutableArray *circleViewArray;
@property (nonatomicstrongNSMutableArray *titleLabelArray;
@property (nonatomicstrongUILabel *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(001313)];
            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(00self.frame.size.width - perWidth, 1);
    self.progressView.center = CGPointMake(self.frame.size.width / 2self.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 / 2self.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(002323)];
        _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


640?wx_fmt=gif接口调用:


- (void)viewDidLoad {
    
    [super viewDidLoad];

    // 初始化
    _hqlStepView = [[HQLStepView alloc] initWithFrame:CGRectMake(0200self.view.frame.size.width, 60) titlesArray:@[@"第一步"@"第二步"@"第三步"] stepIndex:0];
    [self.view addSubview:_hqlStepView];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];

    // 设置当前步骤,步骤索引=数组索引
    [_hqlStepView setStepIndex:0 animation:YES];
}


640?wx_fmt=gif效果:


640?wx_fmt=other


因为 UIProgressView 实现的水平进度条高度值默认为1,设置frame是无效的。可以通过仿射变换的方式增加它的高度。


640?wx_fmt=gif第三方框架


https://github.com/instant-solutions/ISTimeline


https://github.com/kf99916/TimelineTableViewCell


640?wx_fmt=gif参考:


iOS 自定义步骤进度条


http://www.cnblogs.com/5ishare/p/5044447.html?utm_source=tuicool&utm_medium=referral


XFStepProgress

https://github.com/levenwhf/XFStepProgress


 推荐↓↓↓ 

640?wx_fmt=png

?16个技术公众号】都在这里!

涵盖:程序员大咖、源码共读、程序员共读、数据结构与算法、黑客技术和网络安全、大数据科技、编程前端、Java、Python、Web编程开发、Android、iOS开发、Linux、数据库研发、幽默程序员等。

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/olsQ93038o99S/article/details/84537455

智能推荐

信息系统项目管理师必背核心考点(二十七)关键路径法(CPM)_关键路径的考点-程序员宅基地

文章浏览阅读1k次。科科过为您带来软考信息系统项目管理师核心重点考点(二十七)关键路径法(CPM),内含思维导图+真题_关键路径的考点

【氧化镓】Ga2O3 MOSFET器件的单SEB机制TCAD研究-程序员宅基地

文章浏览阅读636次,点赞8次,收藏9次。本文是一篇关于氧化镓(Ga2O3)金属氧化物半导体场效应晶体管(MOSFET)在单粒子烧毁(single event burnout, SEB)事件中的机制研究的文章。文章通过使用技术计算机辅助设计(TCAD)模拟来探究侧向耗尽型氧化镓MOSFET设备在SEB中的敏感区域和安全操作电压,并提出了辐射损伤机制。

分享一个关于生鲜电商的竞品分析报告-程序员宅基地

文章浏览阅读1.8k次,点赞3次,收藏17次。1.行业分析近年来,生鲜电商在资本的加持下迅速发展。2019年随着资本态度趋于保守,加之多数生鲜电商尚未实现规模化盈利,多家生鲜电商平台开始出现危机,生鲜电商行业又迎来新一轮洗牌,与此同时..._电子商务数据分析测试一一家生鲜店铺为进一步占据更多市场份额,需要对其竞争对手

微信小程序nodejs+uniapp课堂学习资料分享系统+vue-程序员宅基地

文章浏览阅读225次,点赞5次,收藏9次。VScode是我们最常用的网页编辑器,通过日常学习,我们基本熟练运用,在完成项目的过程中,我们可以更加节省时间。网络已成为学生越来越重要的学习渠道和知识来源,交互式教学(学习)的内涵和形式将会随着技术的升级,理念的更新而不断完善和丰富,它的教学组织方法也将朝着多样性的方向发展,交互式教学平台构建的理念、技术和方法也会随之变化,不断趋于完善。作为当代大学生,今后要担负起重任,成为国家的栋梁之材,就必须在大学学习阶段获得充足的知识储备和能力的提高,而充分的课外知识学习就是实现这一目标的重要途径之一。

小程序(六)后端 登录+注册_小程序怎么在后端api系统注册登录-程序员宅基地

文章浏览阅读1.1k次。小程序(六)后端 登录+注册业_小程序怎么在后端api系统注册登录

CocosCreator 之 Tween缓动系统的使用_cocos tween-程序员宅基地

文章浏览阅读981次,点赞7次,收藏2次。在CocosCreator 3.x版本后,Tween缓动系统代替了原有的Action动作。官方使用缓动系统的主要目的之一是用于解决离线动画无法满足需求时的动态动画问题。// 延迟两秒// 2秒钟移动到(0, 300, 0)的位置,并缩放由1为0// 执行回调console.log("运行结束");})// 开始运行当前缓动对象.start();使用缓动,主要通过tween函数来构建Tween实例化对象。它非Tween。_cocos tween

随便推点

删除一个英文字符串当中所有的字母d C语言学习日志#3_编写程序,删除输入字符串中的字符d-程序员宅基地

文章浏览阅读284次。【代码】删除一个英文字符串当中所有的字母d C语言学习日志#3。_编写程序,删除输入字符串中的字符d

python实现接口上传文件的两种方法_python上传文件-程序员宅基地

文章浏览阅读3.7w次,点赞12次,收藏76次。文件上传:上传图片的类型是file,这里没有用到头部信息import requestsdef sendImg(img_path, img_name, img_type='image/jpeg'): """ :param img_path:图片的路径 :param img_name:图片的名称 :param img_type:图片的类型,这里写的是image/jpeg,也可以是png/..._python上传文件

2019FME博客大赛——为数据而生的FME - 腾讯人口迁徙数据爬取与分析-程序员宅基地

文章浏览阅读1w次,点赞17次,收藏60次。参赛单元:互联网、大数据及云计算作者:刘啸单位:上海垣观数据科技有限公司随着信息技术的发展,大数据的概念越来越引发人们的关注,各种有关于城市的新数据类型也不断涌现,为客观认识城市系统并总结其发展规律提供了重要机遇。本文结合腾讯人口迁徙数据爬取与分析案例,不仅展示了如何利用FME进行支撑城市规划决策的数据分析,也展示了以FME为核心的从数据获取、数据清洗、数据分析、数据可视化等全生...

java实现(如:WORD、EXCEL、PPT、ZIP 等)文件在线预览功能-程序员宅基地

文章浏览阅读1.6w次,点赞9次,收藏72次。附带有各类型附件(如:word,excel,ppt,zip等),用户希望能够有在线预览附件的功能。日常开发中常见的文件格式有pdf,word,Excel,PPT,Html,txt,图片等。pdf,Html,txt,图片这种实现在线预览非常简单,有一些前端的插件可以满足要求。word,Excel,PPT如果要实现在线预览则更难一些。...

解决冰点文库不能使用问题!冰点文库下载器最新版_冰点下载网络链接失败怎么办-程序员宅基地

文章浏览阅读3.2w次。百度文库文档免费下载PDF版,老版本不能用了,百度云盘链接: https://pan.baidu.com/s/1t6owKPMX966FH7uDm2b6GQ 密码: 3mgc _冰点下载网络链接失败怎么办

virt-manager+virt-install 制作windows7+10-qcow2镜像_windows 7 cow2-程序员宅基地

文章浏览阅读3.8k次。制作windows7+10-qcow2镜像1.工具准备:vmware 14、virtio-win-0.1.141.iso(虚拟驱动)、cloudbase-init(虚拟机初始化工具)、win-7.iso/win-10.iso。vmware:https://www.vmware.com/cn/products/workstation-pro/workstation-pro-evaluation...._windows 7 cow2

推荐文章

热门文章

相关标签