您好,登錄后才能下訂單哦!
今天就跟大家聊聊有關怎么在iOS中實現步驟進度條功能,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結了以下內容,希望大家根據這篇文章可以有所收獲。
HQLStepView.h 文件
#import <UIKit/UIKit.h> @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中實現步驟進度條功能有進一步的了解嗎?如果還想了解更多知識或者相關內容,請關注億速云行業資訊頻道,感謝大家的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。