IOS第16天(1,Quartz2D基本图像绘制)

时间:2025-02-06 18:06:08

***************基本图像绘制 画线

#import "HMLineView.h"

@implementation HMLineView

- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
} // Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation. /**
* 什么调用:当你视图第一次显示的时候就会调用
* 作用:绘图
* @param rect = self.bounds
*/
- (void)drawRect:(CGRect)rect
{
// 1.获取上下文
CGContextRef ctx = UIGraphicsGetCurrentContext(); // 2.拼接路径
UIBezierPath *path = [UIBezierPath bezierPath]; CGPoint startP = CGPointMake(, );
CGPoint endP = CGPointMake(, );
CGPoint controlP = CGPointMake(, );
[path moveToPoint:startP];
[path addQuadCurveToPoint:endP controlPoint:controlP]; // 3.把路径添加到上下文
CGContextAddPath(ctx, path.CGPath); // 4.渲染上下文到视图
CGContextStrokePath(ctx); } - (void)draw2Line
{
// 1.获取上下文
CGContextRef ctx = UIGraphicsGetCurrentContext(); // 2.拼接路径
UIBezierPath *path = [UIBezierPath bezierPath]; // 设置起点
[path moveToPoint:CGPointMake(, )]; // 添加一条线到某个点
[path addLineToPoint:CGPointMake(, )]; // // 设置起点
// [path moveToPoint:CGPointMake(10, 10)];
//
// // 添加一条线到某个点
// [path addLineToPoint:CGPointMake(125, 100)]; UIBezierPath *path1 = [UIBezierPath bezierPath]; [path1 moveToPoint:CGPointMake(, )]; [path1 addLineToPoint:CGPointMake(, )]; // 3.把路径添加到上下文
CGContextAddPath(ctx, path.CGPath);
CGContextAddPath(ctx, path1.CGPath); // 设置绘图状态
// 设置线宽
CGContextSetLineWidth(ctx, );
CGContextSetLineCap(ctx, kCGLineCapRound);
// CGContextSetRGBStrokeColor(ctx, 1, 0, 0, 1);
[[UIColor redColor] set]; // 4.渲染上下文到视图
CGContextStrokePath(ctx);
} - (void)drawLine
{
// 1.获取上下文
// CGContextRef CG CoreGraphics Ref 引用
// 目前学的上下文都跟UIGraphics有关,以后想直接获取上下文,直接敲一个UIGraphics
CGContextRef ctx = UIGraphicsGetCurrentContext(); // 2.设置绘图信息(拼接路径)
UIBezierPath *path = [UIBezierPath bezierPath]; // 设置起点
[path moveToPoint:CGPointMake(, )]; // 添加一条线到某个点
[path addLineToPoint:CGPointMake(, )];
[path addLineToPoint:CGPointMake(, )];
// 3.把路径添加到上下文
// 直接把UIKit的路径转换成CoreGraphics,CG开头就能转
CGContextAddPath(ctx, path.CGPath); // 4.把上下文渲染到视图
// Stroke描边
CGContextStrokePath(ctx);
} @end

******基本的图形的绘制

#import "HMShapeView.h"

@interface HMShapeView()

@property (nonatomic, weak) UILabel *label;

@end

@implementation HMShapeView

- (UILabel *)label
{
if (_label == nil) {
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(, , , )];
label.text = @"s";
label.font = [UIFont systemFontOfSize:];
label.textColor = [UIColor yellowColor];
label.textAlignment = NSTextAlignmentCenter;
[self addSubview:label];
_label = label;
}
return _label;
} - (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
} - (void)awakeFromNib
{
// self.label;
} // Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation. // 扇形行
- (void)drawRect:(CGRect)rect
{
// Drawing code // 1.获取上下文
CGContextRef ctx = UIGraphicsGetCurrentContext(); // 2.拼接路径
CGPoint center = CGPointMake(, );
CGFloat radius = ;
CGFloat startA = ;
CGFloat endA = M_PI_2;
UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:startA endAngle:endA clockwise:YES]; [path addLineToPoint:center]; // 3.把路径添加到上下文
CGContextAddPath(ctx, path.CGPath); // 4.渲染上下文
// CGContextStrokePath(ctx);
CGContextFillPath(ctx); } - (void)drawArc
{
// 1.获取上下文
CGContextRef ctx = UIGraphicsGetCurrentContext(); // 2.拼接路径
CGPoint center = CGPointMake(, );
CGFloat radius = ;
CGFloat startA = ;
CGFloat endA = M_PI_2;
UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:startA endAngle:endA clockwise:YES]; // 3.把路径添加到上下文
CGContextAddPath(ctx, path.CGPath); // 4.渲染上下文
CGContextStrokePath(ctx);
} // 圆行
- (void)drawCircle
{
// 1.获取上下文
CGContextRef ctx = UIGraphicsGetCurrentContext(); // 2.拼接路径
UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(, , , )]; // 3.把路径添加到上下文
CGContextAddPath(ctx, path.CGPath); // 4.渲染上下文
CGContextStrokePath(ctx); }
// 矩形
- (void)drawRectangle
{
// 1.获取上下文
CGContextRef ctx = UIGraphicsGetCurrentContext(); // 2.拼接路径
UIBezierPath *path = [UIBezierPath bezierPathWithRect:CGRectMake(, , , )];
path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(, , , ) cornerRadius:]; // 3.把路径添加到上下文
CGContextAddPath(ctx, path.CGPath); // 4.渲染上下文
CGContextStrokePath(ctx);
} //三角
- (void)drawSupernene
{
// 1.获取上下文
CGContextRef ctx = UIGraphicsGetCurrentContext(); // 2.拼接路径
UIBezierPath *path = [UIBezierPath bezierPath]; CGPoint startP = CGPointMake(, ); [path moveToPoint:startP]; [path addLineToPoint:CGPointMake(, )]; [path addLineToPoint:CGPointMake(, )]; // 从路径的终点连接到起点
[path closePath];
// [path addLineToPoint:startP]; // 3.把路径添加到上下文
CGContextAddPath(ctx, path.CGPath); [[UIColor blueColor] setFill];
[[UIColor redColor] setStroke]; CGContextSetLineWidth(ctx, ); // 4.渲染上下文
// CGContextStrokePath(ctx);
// CGContextFillPath(ctx);
// 即填充又描边 kCGPathFillStroke
CGContextDrawPath(ctx, kCGPathFillStroke);
} @end