iOS - 图形上下文绘图
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 转圈的进度条。使用简单的图形上下文实现。
注:图形上下文的绘制是根据代码顺序依次绘制的,若顺序颠倒,会出现遮挡现象。

浙公网安备 33010602011771号