怎么在iOS中实现步骤进度条功能-创新互联

今天就跟大家聊聊有关怎么在iOS中实现步骤进度条功能,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

龙口ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为成都创新互联公司的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:13518219792(备注:SSL证书合作)期待与您的合作!

HQLStepView.h 文件

#import 

@interface HQLStepView : UIView

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

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

@end

HQLStepView.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 (nonatomic, copy) NSArray *titlesArray;
@property (nonatomic, assign) NSUInteger stepIndex;

@property (nonatomic, strong) UIProgressView *progressView;
@property (nonatomic, strong) NSMutableArray *circleViewArray;
@property (nonatomic, strong) NSMutableArray *titleLabelArray;
@property (nonatomic, strong) UILabel *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(0, 0, 13, 13)];
  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(0, 0, self.frame.size.width - perWidth, 1);
 self.progressView.center = CGPointMake(self.frame.size.width / 2, self.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 / 2, self.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(0, 0, 23, 23)];
 _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

接口调用:

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

- (void)viewDidAppear:(BOOL)animated {
 [super viewDidAppear:animated];
 
 // 设置当前步骤,步骤索引=数组索引
 [_hqlStepView setStepIndex:0 animation:YES];
}

看完上述内容,你们对怎么在iOS中实现步骤进度条功能有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注创新互联网站建设公司行业资讯频道,感谢大家的支持。

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


网页标题:怎么在iOS中实现步骤进度条功能-创新互联
本文网址:http://myzitong.com/article/dohscd.html