iOS如何自定义步骤进度条实例详解

iOS如何自定义步骤进度条实例详解,第1张

概述iOS如何自定义步骤进度条实例详解 前言 最近新项目要做入驻功能,其中包括一个入住流程,类似登录或者注册流程如下图. 之前想着用自己绘图来做,可是又懒不想多写代码,所以就想着能不能用进度条来做. 实现方法如下: 1.用进度条做的首先要解决的是进度条的高度问题,可以通过仿射变换来扩大高度. progressView.transform = CGAffineTransformMakeScale(1.0f,2.0f); 2.用进度条要设置进度progress要与按钮对应 通过步骤的索引来改变进度的值和按钮的图片.由于按钮的左右有间隔所以

前言

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

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

实现方法如下:

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 <UIKit/UIKit.h>@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<TitleArray.count; i++) {  //图片按钮 UIbutton *btn=[UIbutton buttonWithType:UIbuttonTypeCustom]; [btn setimage:[UIImage imagenamed:@"0.png"] forState:UIControlStatenormal]; [btn setimage:[UIImage imagenamed:@"1.png"] forState:UIControlStateSelected]; btn.frame=CGRectMake(_btnWIDth/2+_btnWIDth*i-imgBtnWIDth/2,imgBtnWIDth,imgBtnWIDth); btn.selected=YES; [stepProgressVIEw addSubvIEw:btn]; [stepProgressVIEw.imgBtnArray addobject:btn]; //文字 UILabel *TitleLabel=[[UILabel alloc]initWithFrame:CGRectMake(btn.center.x-_btnWIDth/2,frame.size.height-20,_btnWIDth,20)]; TitleLabel.text=[TitleArray objectAtIndex:i]; [TitleLabel setTextcolor:[UIcolor blackcolor]]; TitleLabel.textAlignment=NSTextAlignmentCenter; TitleLabel.Font=[UIFont systemFontOfSize:18]; [stepProgressVIEw addSubvIEw:TitleLabel]; } stepProgressVIEw.stepIndex=-1; return stepProgressVIEw;}-(voID)setStepIndex:(NSInteger)stepIndex{// 默认为-1 小于-1为-1 大于总数为总数 _stepIndex=stepIndex<-1?-1:stepIndex; _stepIndex=stepIndex >=_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];

补充:上面的代码有一个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如何自定义步骤进度条实例详解全部内容,希望文章能够帮你解决iOS如何自定义步骤进度条实例详解所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1110020.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存