一直沒有一個好的繪制視圖的筆記郭宝,今天整理一下重虑,分享給大家:
不多說,直接上代碼。
一频鉴、核心繪圖(C語言風(fēng)格調(diào)用函數(shù))
- (void)drawRect:(CGRect)rect {
//獲取上下文環(huán)境(獲取畫板)
CGContextRef context = UIGraphicsGetCurrentContext();
//把畫筆移動到起始點(diǎn)
CGContextMoveToPoint(context, 100, 100);
CGContextAddLineToPoint(context, 250, 250);
CGContextAddLineToPoint(context, 150, 450);
CGContextAddLineToPoint(context, 50, 300);
CGContextAddLineToPoint(context, 100, 100); //設(shè)置描邊的顏色
CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
//描邊
CGContextStrokePath(context);
//設(shè)置填充顏色
CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor);
//填充
CGContextFillPath(context);
}
//drawRect:方法會在創(chuàng)建View的實(shí)例時栓辜,系統(tǒng)自動調(diào)用一次,絕對不可以手動調(diào)用該方法.
二垛孔、 貝塞爾曲線UIBezierPath 繪圖
- 繪制曲線
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(150, 50)];
[path addCurveToPoint:CGPointMake(50, 150) controlPoint1:CGPointMake(50, 50) controlPoint2:CGPointMake(150, 150)];
[path addCurveToPoint:CGPointMake(150, 250) controlPoint1:CGPointMake(150, 150) controlPoint2:CGPointMake(50, 250)];
[[UIColor redColor]setStroke];
[path stroke]; - 繪制矩形
UIBezierPath *path = [UIBezierPath bezierPathWithRect:CGRectMake(20, 300, 200, 15)];
path.lineWidth = 4;//描邊線寬
path.lineJoinStyle = kCGLineJoinBevel;//焦點(diǎn)的樣式
path.lineCapStyle = kCGLineCapSquare;//線兩端的樣式
[[UIColor blueColor] setStroke];//設(shè)置描邊色
[path stroke]; - 繪制圓角矩形
UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(20, 330, 200, 150) cornerRadius:75];
[path stroke]; - 繪制橢圓
UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(20, 330, 200, 150)];
[[UIColor greenColor] setFill];//設(shè)置填充色
[path fill];
三藕甩、 用bezierPath繪制八卦
CGPoint center = CGPointMake(self.frame.size.width * 0.5, self.frame.size.height * 0.5);
CGFloat radius = self.frame.size.width * 0.5 - 50;
UIBezierPath *path = [UIBezierPath bezierPath];
[path addArcWithCenter:center radius:radius startAngle:M_PI_2 * 3 endAngle:M_PI_2 clockwise:YES];
[path addCurveToPoint:CGPointMake(center.x , center.y - radius) controlPoint1:CGPointMake(center.x + radius , center.y) controlPoint2:CGPointMake(center.x - radius , center.y)];
[[UIColor whiteColor] setStroke];
[[UIColor blackColor] setFill];
path.lineWidth = 5;
[path stroke];
[path fill];
UIBezierPath *path2 = [UIBezierPath bezierPath];
[path2 addArcWithCenter:center radius:radius startAngle:M_PI_2 endAngle:M_PI_2* 3 clockwise:YES];
[path2 addCurveToPoint:CGPointMake(center.x , center.y + radius) controlPoint1:CGPointMake(center.x - radius , center.y) controlPoint2:CGPointMake(center.x + radius , center.y)];
[[UIColor blackColor] setStroke];
[[UIColor whiteColor] setFill];
path2.lineWidth = 5;
[path2 stroke];
[path2 fill];
四、繪制DownLoad圖形
自定義UIView里的draw方法里
- (void)drawRect:(CGRect)rect {
CGPoint center = CGPointMake(self.frame.size.width * 0.5, self.frame.size.height * 0.5);
CGFloat radius = MIN(self.frame.size.width * 0.5, self.frame.size.height * 0.5) - 4;
UIBezierPath *bezierPath = [UIBezierPath bezierPath]周荐;
[bezierPath addArcWithCenter:center radius:radius startAngle:M_PI_2 * 3 endAngle:M_PI_2 * 3 + M_PI * 2 * self.progressValue clockwise:YES];
bezierPath.lineWidth = 8;
[[UIColor blueColor]setFill];
[bezierPath fill];
}