简介
UIBezierPath类创建基于矢量的路径,例如椭圆或者矩形,或者有多个直线和曲线段组成的形状。
UIBezierPath是UIKit中的一个关于图形绘制的类,是通过Quartz 2D也就是CG(Core Graphics)CGPathRef的封装得到的,从高级特性支持来看不及CG。
使用
UIBezierPath
,你只能在当前图形上下文
中绘制。CGContextRef
即图形上下文。
1.重写UIView的drawRect
方法,在该方法里便可得到context;
2.调用UIGraphicsBeginImageContextWithOptions
方法得到
drawRect:
触发触发时机
1、当view第一次显示
到屏幕上时;
2、当调用view的setNeedsDisplay
或者setNeedsDisplayInRect:
方法时。
绘制流程
- 初始化一个 UIBezierPath 对象
- 设置相关的属性;
- 调用 -moveToPoint: 方法初始线段的起点;
- 添加线段或者曲线段去构建一个或者多个子路径;
- (void)drawRect:(CGRect)rect // 重写drawRect方法
{
// 1.初始化图形相应的UIBezierPath对象
UIBezierPath* aPath = [UIBezierPath bezierPathWithRect:CGRectMake(0, 0, 200, 200)]; // 2.
// 2.设置一些修饰属性
aPath.lineWidth = 8.0;
//路径的终点形状,
aPath.lineCapStyle = kCGLineCapRound;
//路径的连接点形状
aPath.lineJoinStyle = kCGLineCapRound;
UIColor *color = [UIColor colorWithRed:0 green:0 blue:0.7 alpha:1];
[color set];
// 3.起点
[aPath moveToPoint:CGPointMake(20, 20)];
// 4.绘制线条
[aPath addLineToPoint:CGPointMake(100, 100)];
[aPath stroke]; // 渲染,完成绘制
}
创建 UIBezierPath
- 创建并且返回一个新的
UIBezierPath
对象
+ (instancetype) bezierPath;
- 通过一个矩形, 创建并且返回一个新的
UIBezierPath
对象
+ (instancetype)bezierPathWithRect:(CGRect)rect;
- 通过一个指定的矩形中的椭圆形, 创建并且返回一个新的
UIBezierPath
对象
+ (instancetype)bezierPathWithOvalInRect:(CGRect)rect;
- 根据一个圆角矩形, 创建并且返回一个新的
UIBezierPath
对象
+ (instancetype) bezierPathWithRoundedRect:(CGRect)rect
cornerRadius:(CGFloat)cornerRadius;
- 根据一个圆角矩形, 创建并且返回一个新的
UIBezierPath
对象
+ (instancetype) bezierPathWithRoundedRect:(CGRect)rect
byRoundingCorners:(UIRectCorner)corners
cornerRadii:(CGSize)cornerRadii;
- 通过一个圆弧, 创建并且返回一个新的
UIBezierPath
对象
+ (instancetype) bezierPathWithArcCenter:(CGPoint)center
radius:(CGFloat)radius
startAngle:(CGFloat)startAngle
endAngle:(CGFloat)endAngle
clockwise:(BOOL)clockwise;
- 通过一个 CGPath, 创建并且返回一个新的
UIBezierPath
对象
+ (instancetype) bezierPathWithCGPath:(CGPathRef)CGPath;
- 创建并返回一个新的BezierPath, 这个 BezierPath 的方向是原 BezierPath 的反方向
- (UIBezierPath *) bezierPathByReversingPath;
构造路径
- 将
UIBezierPath
对象的currentPoint
移动到指定的点
- (void)moveToPoint:(CGPoint)point;
- 在当前
子路径
中追加一条直线
- (void)addLineToPoint:(CGPoint)point;
- 在当前
子路径
中追加一条圆弧
- (void)addArcWithCenter:(CGPoint)center
radius:(CGFloat)radius
startAngle:(CGFloat)startAngle
endAngle:(CGFloat)endAngle
clockwise:(BOOL)clockwise NS_AVAILABLE_IOS(4_0);
- 在当前
子路经
中追加一条二次贝塞尔曲线
- (void)addQuadCurveToPoint:(CGPoint)endPoint
controlPoint:(CGPoint)controlPoint;
- 在当前
子路经
中追加一条三次贝塞尔曲线
- (void)addCurveToPoint:(CGPoint)endPoint
controlPoint1:(CGPoint)controlPoint1
controlPoint2:(CGPoint)controlPoint2;
- 关闭当前
子路经
- (void)closePath;
- 删除
UIBezierPath
对象中的所有点, 效果也就等同于删除了所有子路经
- (void)removeAllPoints;
- 将指定
UIBezierPath
中的内容添加到当前UIBezierPath
对象中
- (void)appendPath:(UIBezierPath *)bezierPath;
UIBezierPath
中的CGPath
对象
@property(nonatomic) CGPathRef CGPath;
- 绘图路径中的当前点
@property(nonatomic, readonly) CGPoint currentPoint;
绘图属性
- 线宽
@property(nonatomic) CGFloat lineWidth;
- 曲线终点样式
@property(nonatomic) CGLineCap lineCapStyle;
// CGPath.h
typedef CF_ENUM(int32_t, CGLineCap) {
kCGLineCapButt,
kCGLineCapRound,
kCGLineCapSquare
};
- 曲线连接点样式
@property(nonatomic) CGLineJoin lineJoinStyle;
// CGPath.h
typedef CF_ENUM(int32_t, CGLineJoin) {
kCGLineJoinMiter,
kCGLineJoinRound,
kCGLineJoinBevel
};
- 内角和外角距离
@property(nonatomic) CGFloat miterLimit;
2452150-d6647c67c61e87c6.png
- 渲染精度
@property(nonatomic) CGFloat flatness;
- 是否使用基偶填充规则 -- 详细介绍
@property(nonatomic) BOOL usesEvenOddFillRule;
- 虚线
- (void)setLineDash:(const CGFloat *)pattern
count:(NSInteger)count
phase:(CGFloat)phase;
- 重新获取虚线的模式
- (void)getLineDash:(CGFloat *)pattern
count:(NSInteger *)count
phase:(CGFloat *)phase;
绘制路径
- 填充路径
- (void)fill;
- 使用混合模式进行填充
- (void)fillWithBlendMode:(CGBlendMode)blendMode
alpha:(CGFloat)alpha;
- 绘制路径
- (void)stroke;
- 使用混合模式进行填充
- (void)strokeWithBlendMode:(CGBlendMode)blendMode
alpha:(CGFloat)alpha;
剪切路径
- 剪切路径
- (void)addClip;
Hit Detection
- 是否包含某个点
- (BOOL) containsPoint:(CGPoint)point;
- 路径是否为空
@property (readonly, getter=isEmpty) BOOL empty;
- 路径覆盖的矩形区域
@property (nonatomic, readonly) CGRect bounds;
Apply Transform
- (void)applyTransform:(CGAffineTransform)transform;
实例
Demo: https://github.com/iOSlixiang/Animations.git