drawRect调用场景
- 视图第一次显示的时候会调用。这个是由系统自动调用的,主要是在
UIViewController
中loadView
和viewDidLoad
方法调用之后; - 如果在
UIView
初始化时没有设置rect
大小,将直接导致drawRect
不被自动调用 - 该方法在调用
sizeThatFits
后被调用,所以可以先调用sizeToFit
计算出size
,然后系统自动调用drawRect:
方法; - 通过设置
contentMode
属性值为UIViewContentModeRedraw
,那么将在每次设置或更改frame
的时候自动调用drawRect:
; - 直接调用
setNeedsDisplay
,或者setNeedsDisplayInRect:
触发drawRect:
,但是有个前提条件是rect不能为0;
drawRect使用注意事项
- 如果子类直接继承自UIView,则在drawRect 方法中不需要调用super方法。若子类继承自其他View类则需要调用super方法以实现重绘
- 若使用UIView绘图,只能在
drawRect:
方法中获取绘制视图的contextRef
。在其他方法中获取的contextRef
都是不生效的; -
drawRect:
方法不能手动调用,需要调用实例方法setNeedsDisplay
或者setNeedsDisplayInRect
,让系统自动调用该方法; - 若使用
CALayer
绘图,只能在drawInContext :
绘制,或者在delegate
方法中进行绘制,然后调用setNeedDisplay
方法实现最终的绘制;
-(void)drawRect:(CGRect)rect { CGContextRef contextRef = UIGraphicsGetCurrentContext(); UIBezierPath *path0 = [UIBezierPath bezierPathWithArcCenter:CGPointMake(self.frame.size.width/2, self.frame.size.height/2) radius:self.frame.size.width/2 startAngle:-M_PI_2 endAngle:M_PI_2 * 3 clockwise:YES]; CGContextAddPath(contextRef, path0.CGPath); CGContextFillPath(contextRef); CGContextSetRGBFillColor(contextRef, 50.0/255.0, 50.0/255.0, 50.0/255.0, 0.8); CGContextStrokePath(contextRef); CGContextRef contextRef1 = UIGraphicsGetCurrentContext(); UIBezierPath *path1 = [UIBezierPath bezierPathWithArcCenter:CGPointMake(self.frame.size.width/2, self.frame.size.height/2) radius:self.frame.size.width/2 - 1 startAngle:-M_PI_2 endAngle:M_PI_2 * 3 clockwise:YES]; CGContextAddPath(contextRef1,path1.CGPath); CGContextSetRGBStrokeColor(contextRef1, 50.0/255.0, 50.0/255.0, 50.0/255.0, 1); CGContextSetLineWidth(contextRef1, 2); CGContextStrokePath(contextRef1); CGContextRef contextRef2 = UIGraphicsGetCurrentContext(); UIBezierPath *path2 = [UIBezierPath bezierPathWithArcCenter:CGPointMake(self.frame.size.width/2, self.frame.size.height/2) radius:self.frame.size.width/2 - 1 startAngle:-M_PI_2 endAngle:_progress/_signProgress * M_PI * 2 - M_PI_2 clockwise:YES]; CGContextAddPath(contextRef2,path2.CGPath); CGContextSetStrokeColorWithColor(contextRef2, [UIColor whiteColor].CGColor); CGContextSetLineWidth(contextRef2, 3); CGContextStrokePath(contextRef2); } -(instancetype)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { self.backgroundColor = [UIColor clearColor]; [self setUpSubViews]; } return self; } -(void)setUpSubViews { self.label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)]; _label.textColor = [UIColor whiteColor]; _label.font = [UIFont systemFontOfSize:13]; _label.textAlignment = 1; _label.layer.zPosition = 3; _label.backgroundColor = [UIColor clearColor]; [self addSubview:self.label]; } -(void)upDateCircleProgress:(CGFloat) progress { self.label.text = [NSString stringWithFormat:@"%.0f",self.signProgress - progress 1]; self.progress = progress; [self setNeedsDisplay]; }
这是一个简单的progressView 转圈的进度条。使用简单的图形上下文实现。