先来看看我机智的呆呆....做出投篮动作,造台阶一个犯规
OK!进入正题,哈哈😀
UIBezierPath简介
从名字上可以看得出这是一个贝塞尔路径,可以绘制直线,曲线,以及各种几何路径,它是基于CoreGraphics中path相关的一个封装,可以直接在
drawRect
方法中绘制出想要的几何图形,也可以配合CAShapeLayer来展示,待会我会在drawRect
里面演示,与CAShapeLayer的配合使用,我之后会用一个更好的实例来演示。
主要的方法
//创建一个矩形路径
+ (instancetype)bezierPathWithRect:(CGRect)rect;
//创建一个内切圆或椭圆的路径
+ (instancetype)bezierPathWithOvalInRect:(CGRect)rect;
//创建一个矩形路径,并设置圆角
+ (instancetype)bezierPathWithRoundedRect:(CGRect)rect cornerRadius:(CGFloat)cornerRadius;
//创建一个矩形路径,并设置圆角方向,以及圆角大小
+ (instancetype)bezierPathWithRoundedRect:(CGRect)rect byRoundingCorners:(UIRectCorner)corners cornerRadii:(CGSize)cornerRadii;
//创建一个圆弧路径,参数(`center`:圆心 、`radius`:半径 、`startAngle `:起始角度 、`endAngle `:结束角度 、`clockwise`:YES顺时针/NO逆时针)。
+ (instancetype)bezierPathWithArcCenter:(CGPoint)center radius:(CGFloat)radius startAngle:(CGFloat)startAngle endAngle:(CGFloat)endAngle clockwise:(BOOL)clockwise;
//去设置初始点
- (void)moveToPoint:(CGPoint)point;
//添加直线
- (void)addLineToPoint:(CGPoint)point;
//添加三次贝塞尔曲线,两个控制点
- (void)addCurveToPoint:(CGPoint)endPoint controlPoint1:(CGPoint)controlPoint1 controlPoint2:(CGPoint)controlPoint2;
//添加二次贝塞尔曲线,一个控制点
- (void)addQuadCurveToPoint:(CGPoint)endPoint controlPoint:(CGPoint)controlPoint;
//添加圆弧
- (void)addArcWithCenter:(CGPoint)center radius:(CGFloat)radius startAngle:(CGFloat)startAngle endAngle:(CGFloat)endAngle clockwise:(BOOL)clockwise
//闭合路径,在起点跟终点添加一条直线
- (void)closePath;
//路径拼接
- (void)appendPath:(UIBezierPath *)bezierPath;
//返回一个与当前路径相反的新的贝塞尔路径对象
- (UIBezierPath *)bezierPathByReversingPath
//添加虚线
- (void)setLineDash:(nullable const CGFloat *)pattern count:(NSInteger)count phase:(CGFloat)phase;
//去填充所绘制的区域
- (void)fill;
//去绘制路径
- (void)stroke;
属性
//拿到CGPath
@property(nonatomic) CGPathRef CGPath;
//画笔当前的位置
@property(nonatomic,readonly) CGPoint currentPoint;
//线宽
@property(nonatomic) CGFloat lineWidth;
//终点类型
@property(nonatomic) CGLineCap lineCapStyle;
typedef CF_ENUM(int32_t, CGLineCap) {
kCGLineCapButt,
kCGLineCapRound,
kCGLineCapSquare
};
//交叉点类型
@property(nonatomic) CGLineJoin lineJoinStyle;
typedef CF_ENUM(int32_t, CGLineJoin) {
kCGLineJoinMiter,
kCGLineJoinRound,
kCGLineJoinBevel
};
//最大斜接长度,只有lineJoin属性为kCALineJoinMiter时miterLimit才有效
@property(nonatomic) CGFloat miterLimit
//指定even-odd(奇偶)规则是否在path可用,奇数在path内部,偶数在外部,所以path交汇处是偶数,是不会绘制的
@property(nonatomic) BOOL usesEvenOddFillRule;
举个栗子
- (void)drawRect:(CGRect)rect{
[[UIColor redColor] setStroke];
[[UIColor greenColor] setFill];
//椭圆
[self addPath:
[UIBezierPath bezierPathWithRoundedRect:CGRectMake(20, 20, 100, 50) cornerRadius:25]];
//矩形
[self addPath:
[UIBezierPath bezierPathWithRect:CGRectMake(140, 20, 100, 50)]];
//内切椭圆
[self addPath:
[UIBezierPath bezierPathWithOvalInRect:CGRectMake(260, 20, 100, 50)]];
//换色
[[UIColor lightGrayColor] setStroke];
[[UIColor blueColor] setFill];
//圆
[self addPath:
[UIBezierPath bezierPathWithOvalInRect:CGRectMake(20, 90, 100, 100)]];
//左上右下圆角
[self addPath:
[UIBezierPath bezierPathWithRoundedRect:CGRectMake(140, 90, 100, 100) byRoundingCorners:UIRectCornerTopLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(50, 0)]];
//圆弧
UIBezierPath*cPath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(310, 140) radius:50 startAngle:M_PI_2 endAngle:0 clockwise:YES];
cPath.lineWidth = 4;
[cPath stroke];
//换色
[[UIColor purpleColor] setStroke];
[[UIColor orangeColor] setFill];
//扇形
UIBezierPath *sPath = [UIBezierPath bezierPath];
[sPath moveToPoint:CGPointMake(70, 260)];
[sPath addArcWithCenter:CGPointMake(70, 260) radius:50 startAngle:-M_PI*2/3.f endAngle:-M_PI_4 clockwise:YES];
[self addPath:sPath];
[sPath closePath];
//二次贝塞尔
UIBezierPath *towPath = [UIBezierPath bezierPath];
[towPath moveToPoint:CGPointMake(150, 210)];
[towPath addQuadCurveToPoint:CGPointMake(150, 310) controlPoint:CGPointMake(250, 260)];
[towPath stroke];
//三次贝塞尔
UIBezierPath *trePath = [UIBezierPath bezierPath];
[trePath moveToPoint:CGPointMake(260, 260)];
[trePath addCurveToPoint:CGPointMake(360, 260) controlPoint1:CGPointMake(285, 210) controlPoint2:CGPointMake(335, 310)];
[trePath stroke];
//交汇圆
UIBezierPath *yPath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(20, 330, 100, 100)];
[yPath appendPath:[UIBezierPath bezierPathWithOvalInRect:CGRectMake(80, 330, 100, 100)]];
yPath.usesEvenOddFillRule = YES;
[self addPath:yPath];
}
- (void)addPath:(UIBezierPath *)path{
path.lineWidth = 4;
[path stroke];
[path fill];
}
基本用法大致就这么多,本身也不是什么很难的东西,就介绍到这里吧😀