iOS绘图 - UIBezierPath详解
2016-11-17 本文已影响578人
莫须有恋
上一篇文章只是简单介绍了绘图的几种方法,你可以根据你的项目选择不同的绘图方法。通过本篇对UIBezierPath有一个详细的了解,帮助你更好的使用UIBezierPath。
学习UIBezierPath之前,我感觉最好是先将UIBezierPath的头文件定义先熟悉一遍,大概了解这个类的属性和方法。
UIBezierPath的工厂方法:
初始化一个贝塞尔对象
+ (instancetype)bezierPath;
初始化一个矩形贝塞尔
+ (instancetype)bezierPathWithRect:(CGRect)rect;
初始化一个矩形的内切圆曲线,想要一个圆的话就传入一个正方形Rect
+ (instancetype)bezierPathWithOvalInRect:(CGRect)rect;
初始化带圆角的贝塞尔,可以用来为UIView扩展添加圆角的方法
//cornerRadius是圆角的半径大小,传入的值最大为rect最长边的一半。rect为正方形传入一半宽时也可以得到一个圆形。常用于绘制圆角
+ (instancetype)bezierPathWithRoundedRect:(CGRect)rect cornerRadius:(CGFloat)cornerRadius;
//如果想得到对应方向上的圆角,可以通过corners来指定某个角画成圆角
+ (instancetype)bezierPathWithRoundedRect:(CGRect)rect byRoundingCorners:(UIRectCorner)corners cornerRadii:(CGSize)cornerRadii;
初始化一个圆弧
//center:弧线中心点的坐标
//radius:所画弧线的半径
//startAngle:弧线的起始弧度
//endAngle:弧线的结束弧度
//clockwise:是否是顺时针
+ (instancetype)bezierPathWithArcCenter:(CGPoint)center
radius:(CGFloat)radius
startAngle:(CGFloat)startAngle
endAngle:(CGFloat)endAngle
clockwise:(BOOL)clockwise;
用Core Graphics Path生成一个贝塞尔
+ (instancetype)bezierPathWithCGPath:(CGPathRef)CGPath;
下面我们通过demo进一步了解UIBezierPath
构建path
设置起始点
- (void)moveToPoint:(CGPoint)point;
添加一条直线
- (void)addLineToPoint:(CGPoint)point;
添加一条三次贝塞尔曲线路径,一般和- (void)moveToPoint:(CGPoint)point;配合使用,设置三次贝塞尔曲线的起始点,endPint是曲线的终点,通过两个控制点确定曲线的路径
![](https://img.haomeiwen.com/i1598774/964a5091618fdfbe.png)
//绘制虚线
//pattern:CGFloat pattern[] = { 1.0f, 2.0f };
//count:pattern中的数据个数
//phase: 开始画线型的起始位置
- (void)setLineDash:(nullable const CGFloat *)pattern count:(NSInteger)count phase:(CGFloat)phase;
- (void)getLineDash:(nullable CGFloat *)pattern count:(nullable NSInteger *)count phase:(nullable CGFloat *)phase;
//填充([[UIColor redColor] setFill]; 设置填充颜色,设置后调用填充方法)
- (void)fill;
//描边([[UIColor redColor] setStroke]; 设置描边颜色,设置后调用描边方法)
- (void)stroke;
//设置填充的混合模式
- (void)fillWithBlendMode:(CGBlendMode)blendMode alpha:(CGFloat)alpha;
//设置描边的混合模式
- (void)strokeWithBlendMode:(CGBlendMode)blendMode alpha:(CGFloat)alpha;
//修改当前图形上下文的绘图区域课件,随后的绘图操作都在执行此方法前的区域中呈现
- (void)addClip;