大佬教程收集整理的这篇文章主要介绍了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 <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@R_913_6964@Label]; } 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如何自定义步骤进度条实例详解所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。