标签:splay views delegate init dsd uicolor 实例 div sig
drawRect调用场景
UIViewController
中loadView
和viewDidLoad
方法调用之后;UIView
初始化时没有设置rect
大小,将直接导致drawRect
不被自动调用sizeThatFits
后被调用,所以可以先调用sizeToFit
计算出size
,然后系统自动调用drawRect:
方法;contentMode
属性值为UIViewContentModeRedraw
,那么将在每次设置或更改frame
的时候自动调用drawRect:
;setNeedsDisplay
,或者setNeedsDisplayInRect:
触发drawRect:
,但是有个前提条件是rect不能为0;drawRect使用注意事项
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 转圈的进度条。使用简单的图形上下文实现。
标签:splay views delegate init dsd uicolor 实例 div sig
原文地址:https://www.cnblogs.com/qzCodeDiary/p/11884605.html