资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

如何实现iOS自定义步骤进度条的案例-创新互联

这篇文章给大家分享的是有关如何实现iOS自定义步骤进度条的案例的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

太仓网站建设公司成都创新互联公司,太仓网站设计制作,有大型网站制作公司丰富经验。已为太仓上1000+提供企业网站建设服务。企业网站搭建\成都外贸网站建设要多少钱,请找那个售后服务好的太仓做网站的公司定做!

前言

最近新项目要做入驻功能,其中包括一个入住流程,类似登录或者注册流程如下图。

如何实现iOS自定义步骤进度条的案例

之前想着用自己绘图来做,可是又懒不想多写代码,所以就想着能不能用进度条来做。

实现方法如下:

1.用进度条做的首先要解决的是进度条的高度问题,可以通过仿射变换来扩大高度。

progressView.transform = CGAffineTransformMakeScale(1.0f,2.0f);

2.用进度条要设置进度progress要与按钮对应

通过步骤的索引来改变进度的值和按钮的图片。由于按钮的左右有间隔所以要注意-1、0和最后一个的progress值。

3.扩展

看有一些类似查公交、车站运行的APP有的可以点击站点,所以就用按钮来做,这样可以扩展。

4.代码

//
// StepProgressView.h
// CustomProgress
//
// Created by City--Online on 15/12/12.
// Copyright © 2015年 City--Online. All rights reserved.
//

#import 

@interface StepProgressView : UIView

@property (nonatomic,assign) NSInteger stepIndex;

+(instancetype)progressViewFrame:(CGRect)frame withTitleArray:(NSArray *)titleArray;

@end
//
// StepProgressView.m
// CustomProgress
//
// Created by City--Online on 15/12/12.
// Copyright © 2015年 City--Online. All rights reserved.
//

#import "StepProgressView.h"

static const float imgBtnWidth=18;

@interface StepProgressView ()

@property (nonatomic,strong) UIProgressView *progressView;

//用UIButton防止以后有点击事件
@property (nonatomic,strong) NSMutableArray *imgBtnArray;

@end

@implementation StepProgressView

+(instancetype)progressViewFrame:(CGRect)frame withTitleArray:(NSArray *)titleArray
{
 StepProgressView *stepProgressView=[[StepProgressView alloc]initWithFrame:frame];
 //进度条
 stepProgressView.progressView=[[UIProgressView alloc]initWithFrame:CGRectMake(0, 5, frame.size.width, 10)];
 stepProgressView.progressView.progressViewStyle=UIProgressViewStyleBar;
 stepProgressView.progressView.transform = CGAffineTransformMakeScale(1.0f,2.0f);
 stepProgressView.progressView.progressTintColor=[UIColor redColor];
 stepProgressView.progressView.trackTintColor=[UIColor blueColor];
 stepProgressView.progressView.progress=0.5;
 [stepProgressView addSubview:stepProgressView.progressView];
 
 
 stepProgressView.imgBtnArray=[[NSMutableArray alloc]init];
 float _btnWidth=frame.size.width/(titleArray.count);
 for (int i=0; i=_imgBtnArray.count-1?_imgBtnArray.count-1:stepIndex;
 float _btnWidth=self.bounds.size.width/(_imgBtnArray.count);
 for (int i=0; i<_imgBtnArray.count; i++) {
 UIButton *btn=[_imgBtnArray objectAtIndex:i];
 if (i<=_stepIndex) {
  btn.selected=YES;
 }
 else{
  btn.selected=NO;
 }
 }
 if (_stepIndex==-1) {
 self.progressView.progress=0.0;
 }
 else if (_stepIndex==_imgBtnArray.count-1)
 {
 self.progressView.progress=1.0;
 }
 else
 {
 self.progressView.progress=(0.5+_stepIndex)*_btnWidth/self.frame.size.width;
 }
}

@end

5.使用和效果

NSArray *arr=@[@"区宝时尚",@"区宝时尚",@"时尚",@"区宝时尚",@"时尚"];
 StepProgressView *stepView=[StepProgressView progressViewFrame:CGRectMake(0, 100, self.view.bounds.size.width, 60) withTitleArray:arr];
 stepView.stepIndex=2;
 [self.view addSubview:stepView];

如何实现iOS自定义步骤进度条的案例

补充:上面的代码有一个bug,例如stepIndex=-1时,_stepIndex=并不等-1,原来数组的count返回的是NSUInteger而stepIndex是NSInteger类型,所以需要强制转换一下

stepIndex=stepIndex<-1?-1:stepIndex;
 _stepIndex = stepIndex >= (NSInteger)(_imgBtnArray.count-1) ? _imgBtnArray.count-1:stepIndex;

感谢各位的阅读!关于“如何实现iOS自定义步骤进度条的案例”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

另外有需要云服务器可以了解下创新互联建站www.cdcxhl.com,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


当前文章:如何实现iOS自定义步骤进度条的案例-创新互联
URL链接:http://cdkjz.cn/article/dsecih.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220