绘图API分析

一、坐标系


绘图API分析_第1张图片
image

UIKit使用的是y下坐标系,Core Graphics使用的是y上坐标系。而UIKit指的是UIImage的DrawRect以及UIGraphicsGetCurrentContext()等。Core Graphics指的是以CG开头的API,例如CGContextDrawImage等

其实UIGraphicsGetCurrentContext获取到默认Layer Graphics Context是y下坐标的,所以画线没问题。画图用到CGContextDrawImage,其实CGContextDrawImage使用的是Bitmap Graphics Context这个context是y上坐标系,需要进行坐标转换。

UIGraphicsGetCurrentContext:y下坐标系
UIImage的DrawRect是 :y下坐标系
CGContextDrawImage :y上坐标系
UIGraphicsBeginImageContextWithOptions:y上坐标系

坐标系转换 绘图CoreGraphics转 UIKit
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(context, 0, self.bounds.size.height);
CGContextScaleCTM(context, 1.0, -1.0);

2.相关API分析

UIGraphicsGetCurrentContext:图形上下文
CGContextRef ctx = UIGraphicsGetCurrentContext()

CGContextSetStrokeColorWithColor:替换描边器颜色
CGContextSetStrokeColorWithColor(ctx, [UIColor lightGrayColor].CGColor);

CGContextMoveToPoint:设置起点
CGContextMoveToPoint(ctx, point.x, point.y);

CGContextSetLineWidth:线条宽度
CGContextSetLineWidth(ctx, 0.5);

CGContextSetLineCap:设置线条起点和终点的样式为圆角
CGContextSetLineCap(ctx, kCGLineCapRound);

CGContextSetLineDash:虚线
参数:
phase:第一个虚线绘制的时候跳过多少个点
lengths:CGFloat dash[] = {6, 5}:先绘制6个点,再跳过5个点,如此反复
count:dash数组的长度2
CGContextSetLineDash(ctx, 0.0, dash, 2);
参考:https://www.jianshu.com/p/f6a7e8d3fdb3

CGContextAddLineToPoint:设置终点
CGContextAddLineToPoint(ctx, 300, 100);

CGContextStrokePath:渲染 这条路径出来
CGContextStrokePath(ctx)

UIGraphicsPopContext
UIGraphicsPushContext:压栈当前的绘制对象,生成新的绘制图层

CGPathAddArcToPoint
CGPathAddArcToPoint(CGMutablePathRef _Nullable path, const CGAffineTransform * _Nullable m, CGFloat x1, CGFloat y1, CGFloat x2, CGFloat y2, CGFloat radius)
此方法表示从起点到(x1, y1)再到(x2, y2)的两条直线为切线的弧,radius表示弧的半径

animationWithKeyPath:
transform.translation.x:x位移动
transform.translation.y:y位移动
transform.scale:缩放 1正常大小
strokeStart:位置起点
strokeEnd:位置终点 从起点走到终点沿着路径的方向,跟位移有点不一样。
https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/CoreAnimation_guide/Key-ValueCodingExtensions/Key-ValueCodingExtensions.html

绘图API分析_第2张图片
stroke角度.png

UIBezierPath:
bezierPathWithRect:长方形

你可能感兴趣的:(绘图API分析)