亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

iOS實(shí)現(xiàn)步驟進(jìn)度條功能實(shí)例代碼

 更新時(shí)間:2018年11月07日 09:01:25   作者:獨(dú)木舟的木  
這篇文章主要給大家介紹了關(guān)于iOS實(shí)現(xiàn)步驟進(jìn)度條功能的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

前言

在開(kāi)發(fā)中,我們經(jīng)常在很多場(chǎng)景下需要用到進(jìn)度條,比如文件的下載,或者文件的上傳等。 本文主要給大家介紹的是一個(gè)步驟進(jìn)度條效果,步驟進(jìn)度條效果參考

iOS UIKit 框架中并沒(méi)有提供類似的控件,我們可以使用 UIProgressView、UIView、UILabel 組合實(shí)現(xiàn)步驟進(jìn)度條效果。

  • UIProgressView——實(shí)現(xiàn)水平的進(jìn)度條效果;
  • UIView——把UIView裁剪成圓形,實(shí)現(xiàn)索引節(jié)點(diǎn)效果;
  • UILabel——每個(gè)節(jié)點(diǎn)下面的提示文字。

源碼

將步驟進(jìn)度條封裝成一個(gè) HQLStepView 類,它是 UIView 的子類。

HQLStepView.h 文件

#import <UIKit/UIKit.h>

@interface HQLStepView : UIView

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

// 設(shè)置當(dāng)前步驟
- (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;

 // 進(jìn)度條
 [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];
  
  // 標(biāo)題
  UILabel *label = [[UILabel alloc] init];
  label.text = title;
  label.font = [UIFont systemFontOfSize:14];
  label.textAlignment = NSTextAlignmentCenter;
  [self addSubview:label];
  [self.titleLabelArray addObject:label];
 }
 
 // 當(dāng)前索引數(shù)字
 [self addSubview:self.indicatorLabel];
 }
 return self;
}

// 布局更新頁(yè)面元素
- (void)layoutSubviews {
 NSInteger perWidth = self.frame.size.width / self.titlesArray.count;
 
 // 進(jìn)度條
 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);
 }
 
 // 標(biāo)題
 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;
}

// 設(shè)置當(dāng)前進(jìn)度索引,更新圓形圖片、文本顏色、當(dāng)前索引數(shù)字
- (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;
 // 設(shè)置進(jìn)度條
 [self.progressView setProgress:stepIndex / ((self.titlesArray.count - 1) * 1.0) animated:animation];
 // 設(shè)置當(dāng)前索引數(shù)字
 self.indicatorLabel.text = [NSString stringWithFormat:@"%lu", stepIndex + 1];
 self.indicatorLabel.center = ((UIView *)[self.circleViewArray objectAtIndex:stepIndex]).center;
 }
}

@end

接口調(diào)用:

- (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];
 
 // 設(shè)置當(dāng)前步驟,步驟索引=數(shù)組索引
 [_hqlStepView setStepIndex:0 animation:YES];
}

效果:


因?yàn)?UIProgressView 實(shí)現(xiàn)的水平進(jìn)度條高度值默認(rèn)為1,設(shè)置frame是無(wú)效的??梢酝ㄟ^(guò)仿射變換的方式增加它的高度。

第三方框架

參考:

總結(jié):

以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)腳本之家的支持。

相關(guān)文章

最新評(píng)論