ios自绘UIView进度条及刷新重影解决

xiaoxiao2021-02-28  48

记录下,直接上代码 自绘控件,如果包含子控件的话,刷新会有问题,如本进度条中的文本,显示会有重影。

可以创建控件,然后隐藏控件 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]; }
转载请注明原文地址: https://www.6miu.com/read-80244.html

最新回复(0)