溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

iOS中怎么自定義步驟進(jìn)度條

發(fā)布時(shí)間:2021-07-24 15:33:30 來源:億速云 閱讀:188 作者:Leah 欄目:編程語言

iOS中怎么自定義步驟進(jìn)度條,針對(duì)這個(gè)問題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問題的小伙伴找到更簡(jiǎn)單易行的方法。

實(shí)現(xiàn)方法如下:

1.用進(jìn)度條做的首先要解決的是進(jìn)度條的高度問題,可以通過仿射變換來擴(kuò)大高度。

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

2.用進(jìn)度條要設(shè)置進(jìn)度progress要與按鈕對(duì)應(yīng)

通過步驟的索引來改變進(jìn)度的值和按鈕的圖片。由于按鈕的左右有間隔所以要注意-1、0和最后一個(gè)的progress值。

3.擴(kuò)展

看有一些類似查公交、車站運(yùn)行的APP有的可以點(diǎn)擊站點(diǎn),所以就用按鈕來做,這樣可以擴(kuò)展。

4.代碼

//// StepProgressView.h// CustomProgress//// Created by City--Online on 15/12/12.// Copyright &copy; 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 &copy; 2015年 City--Online. All rights reserved.//#import "StepProgressView.h"static const float imgBtnWidth=18;@interface StepProgressView ()@property (nonatomic,strong) UIProgressView *progressView;//用UIButton防止以后有點(diǎn)擊事件@property (nonatomic,strong) NSMutableArray *imgBtnArray;@end@implementation StepProgressView+(instancetype)progressViewFrame:(CGRect)frame withTitleArray:(NSArray *)titleArray{ StepProgressView *stepProgressView=[[StepProgressView alloc]initWithFrame:frame]; //進(jìn)度條 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, 0, 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{// 默認(rèn)為-1 小于-1為-1 大于總數(shù)為總數(shù) _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=@[@"區(qū)寶時(shí)尚",@"區(qū)寶時(shí)尚",@"時(shí)尚",@"區(qū)寶時(shí)尚",@"時(shí)尚"]; StepProgressView *stepView=[StepProgressView progressViewFrame:CGRectMake(0, 100, self.view.bounds.size.width, 60) withTitleArray:arr]; stepView.stepIndex=2; [self.view addSubview:stepView];

補(bǔ)充:上面的代碼有一個(gè)bug,例如stepIndex=-1時(shí),_stepIndex=并不等-1,原來數(shù)組的count返回的是NSUInteger而stepIndex是NSInteger類型,所以需要強(qiáng)制轉(zhuǎn)換一下

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

關(guān)于iOS中怎么自定義步驟進(jìn)度條問題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注億速云行業(yè)資訊頻道了解更多相關(guān)知識(shí)。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

ios
AI