ios自繪UIView進度條及刷新重影解決

記錄下,直接上代碼
自繪控件,如果包含子控件的話,刷新會有問題,如本進度條中的文本,顯示會有重影。

可以創建控件,然後隱藏控件 hidden = YES; 然後在 drawRect中調用控件的 drawRect方法,可以省去text 的draw函數,去了解一些繪製參數,直接設置控件參數,讓控件自己去繪製,簡單好用。

#import <UIKit/UIKit.h>

@interface VRUpingToolBar : UIView

@property (nonatomic, strong) UILabel *labValue;

@property (nonatomic, assign) CGFloat  progress;

@property (nonatomic, strong) UIColor  *tinkColor;

@property (nonatomic, strong) UIColor  *normalColor;

- (void) setLabsValue:(NSString*)val;

@end

實現文件

#import "VRUpingToolBar.h"

@implementation VRUpingToolBar

- (instancetype) initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if(self){
        [self setupView];
    }
    return self;
}
- (instancetype) init{
    self = [super init];
    if(self){
        [self setupView];
    }
    return self;
}
- (void) setupView{
    _labValue = [UILabel new];
    _labValue.text = @"正在上傳第1/1張照片";
    _labValue.textColor = RGB(255,255,255);
    _labValue.textAlignment = NSTextAlignmentCenter;
    _labValue.font = [UIFont systemFontOfSize:12.0F];
    [self addSubview:_labValue];
    WS(ws);
    [_labValue mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_equalTo(ws);
        make.size.mas_equalTo(ws);
    }];
    _labValue.hidden = YES;//隱藏控件,自己去主動繪製

    _tinkColor = RGB(255, 160, 0);
    _normalColor = RGB(187, 187, 187);
    _progress = 0.2f;
}
- (void)drawRect:(CGRect)rect{
    NSLog(@"upload progress draw %.2f", _progress);
    CGContextRef context = UIGraphicsGetCurrentContext();//context:一塊內存區域,將它看做當前view的畫布就行了。

    CGRect rc;

    [_normalColor set];

    CGContextFillRect(context, self.bounds);
    rc = self.bounds;
    CGRectOffset(rc, -self.width*(1- self.progress), 0);
    rc.size.width = self.width * self.progress;
    [_tinkColor set];
    CGContextFillRect(context, rc);
    CGContextStrokePath(context);
    [_labValue drawRect:rect];//繪製文本
}

-(void) setProgress:(CGFloat)progress{
    _progress = progress;
    NSLog(@"upload progress-- %.2f", progress);
    [self setNeedsDisplay];
}

- (void) setLabsValue:(NSString*)val{
    self.labValue.text = val;
    [self setNeedsDisplay];
}
發佈了121 篇原創文章 · 獲贊 7 · 訪問量 16萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章