美文网首页
进度条写法

进度条写法

作者: 雷霸龙 | 来源:发表于2019-04-10 17:58 被阅读0次

    类似于下图进度条的写法


    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];
    

    相关文章

      网友评论

          本文标题:进度条写法

          本文链接:https://www.haomeiwen.com/subject/qppoiqtx.html