美文网首页
自定义一个ProgressView

自定义一个ProgressView

作者: 今年27 | 来源:发表于2018-01-10 17:11 被阅读7次

    #import@interface LKProgressView : UIView

    @property (nonatomic, assign)CGFloat progress;

    @property (nonatomic, strong)UIImageView* backgroundView;

    @property (nonatomic, strong)UIImageView* trackImageView;

    @end

    #import "LKProgressView.h"

    @interface LKProgressView()

    @property (nonatomic, strong)UIView* innerBg;

    @end

    @implementation LKProgressView

    - (instancetype)init

    {

        self = [super init];

        if (self) {

            [self setupSubViews];

        }

        return self;

    }

    -(void)setupSubViews{

        _backgroundView = [[UIImageView alloc] init];

        [self addSubview:_backgroundView];

        [_backgroundView mas_makeConstraints:^(MASConstraintMaker *make) {

            make.edges.equalTo(self).insets(UIEdgeInsetsZero);

        }];

        UIImage* image = [UIImage imageNamed:@"back_progress"];

        image = [image resizableImageWithCapInsets:UIEdgeInsetsMake(0, 2, 0, 2) resizingMode:UIImageResizingModeTile]; //主要是这个截图最好不需要你去调整截取边缘,我这个还不太标准

        _backgroundView.image = image;

        UIView* innerbgView = [[UIView alloc] init];

        [_backgroundView addSubview:innerbgView];

        [innerbgView mas_makeConstraints:^(MASConstraintMaker *make) {

            make.edges.equalTo(_backgroundView).insets(UIEdgeInsetsMake(2, 2, 2, 2));

        }];

        _trackImageView = [[UIImageView alloc] init];

        [innerbgView addSubview:_trackImageView];

        _innerBg = innerbgView;

        UIImage* track_image = [UIImage imageNamed:@"track_progress"];

        track_image = [track_image resizableImageWithCapInsets:UIEdgeInsetsMake(0, 3, 0, 3) resizingMode:UIImageResizingModeTile];

        _trackImageView.image = track_image;

    //    [_trackImageView mas_makeConstraints:^(MASConstraintMaker *make) {

    //        make.top.equalTo(_innerBg).offset(0);

    //        make.bottom.equalTo(_innerBg).offset(0);

    //        make.left.equalTo(_innerBg).offset(0);

    //        make.width.mas_equalTo(_innerBg.mas_width).multipliedBy(0.01);

    //

    //    }];

        _trackImageView.sd_layout.topEqualToView(innerbgView).leftEqualToView(innerbgView).bottomEqualToView(innerbgView).widthRatioToView(innerbgView, 0.01);

        _backgroundView.layer.cornerRadius = 5.0;

        _backgroundView.layer.masksToBounds = YES;

        _innerBg.layer.cornerRadius = 4.0;

        _innerBg.layer.masksToBounds = YES;

    }

    -(void)setProgress:(CGFloat)progress{

        _progress = progress;

        _trackImageView.sd_resetLayout.topEqualToView(_innerBg).leftEqualToView(_innerBg).bottomEqualToView(_innerBg).widthRatioToView(_innerBg, progress);

    //    [_trackImageView layoutSubviews];

    //    [_trackImageView mas_updateConstraints:^(MASConstraintMaker *make) {

    ////        make.top.equalTo(_backgroundView).offset(0);

    ////        make.bottom.equalTo(_backgroundView).offset(0);

    ////        make.left.equalTo(_backgroundView).offset(0);

    //        make.width.mas_equalTo(_innerBg.mas_width).multipliedBy(progress);

    //    }];

        [_innerBg layoutSubviews];

    //    [_trackImageView mas_remakeConstraints:^(MASConstraintMaker *make) {

    //        make.top.equalTo(_backgroundView).offset(0);

    //        make.bottom.equalTo(_backgroundView).offset(0);

    //        make.left.equalTo(_backgroundView).offset(0);

    //        make.width.mas_equalTo(_backgroundView.mas_width).multipliedBy(progress);

    //

    //    }];

    }

    /*

    // Only override drawRect: if you perform custom drawing.

    // An empty implementation adversely affects performance during animation.

    - (void)drawRect:(CGRect)rect {

        // Drawing code

    }

    */

    @end

    相关文章

      网友评论

          本文标题:自定义一个ProgressView

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