类似于下图进度条的写法
72BA2B16-4872-4D81-83E6-8AD87BE740CF.png
创建一个view类
.h文件中有以下代码
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface YTProgressView : UIView
@property (nonatomic, assign) CGFloat progress;
@end
NS_ASSUME_NONNULL_END
.m文件中有以下代码
#import "YTProgressView.h"
// 每两条细线的间距
#define KProgressPadding 3.0f
// 每条细线的宽度
#define KProgressLineWidth 2.0f
@implementation YTProgressView
- (instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
}
return self;
}
- (void)setProgress:(CGFloat)progress
{
_progress = progress;
CGFloat maxWidth = self.bounds.size.width;
CGFloat heigth = self.bounds.size.height;
int Count = maxWidth / (KProgressPadding + KProgressLineWidth);
int LCount = maxWidth * progress / (KProgressPadding + KProgressLineWidth);
for (int i = 0 ; i < Count; i++) {
CGFloat X = i * (KProgressLineWidth + KProgressPadding);
UIView *Line = [[UIView alloc] initWithFrame:CGRectMake(X, 0, KProgressLineWidth, heigth)];
Line.backgroundColor = (i < LCount) ? [UIColor blueColor]: [UIColor whiteColor];
[self addSubview:Line];
}
}
@end
需要用的话,就在相应的文件中引用就行,代码如下
YTProgressView * chargeProgressView = [[YTProgressView alloc] init];
chargeProgressView.frame = CGRectMake(50, 0, self.view.bounds.size.width, 9);
chargeProgressView.progress = 0.7;
[self.view addSubview:chargeProgressView];
网友评论