之前通过CGContextRef画过一个简单的小黄人,最近又对贝塞尔曲线有点兴趣了,所以通过贝塞尔曲线搞了一些基础的画图.感兴趣的朋友可以共同研究一下(其中一部分是自己感觉比较直白的理解,要是有什么问题,麻烦大佬们指正一下) 废话不多说,直接上干货
画一个简单的小黄人(就是为了熟练的使用一下通过贝塞尔曲线画圆等基础操作)
/*
举个简单的例子,画一个身体正中心的长方形,其余部位一样调用下面的合并方法就行(Demo地址在最下方)
*/
-(void)creatUI{
//小黄人的身体(正方形)贝塞尔曲线画正方形
UIBezierPath *path1 = [UIBezierPath bezierPathWithRect:CGRectMake(self.frame.size.width/4, 300, self.frame.size.width/2, 300)];
//layer加载到view上
[self.layer addSublayer:[self creatCAShapeLayer:[UIColor yellowColor] and:path1]];
}
/*
绘制圆或弧形(小黄人身上所有的圆/圆弧都在此)
startLocation:起始的弧度(3点的方向为圆的起始点方向)
endPoint:结束的弧度
controlPoint:半圆的圆点
clockwise:YES为顺时针/NO为逆时针
radius:半径
*/
-(UIBezierPath*)creatSemicircle:(CGFloat)startLocation and:(CGFloat)endLocation and:(CGPoint)controlPoint and:(BOOL)clockwise and:(CGFloat)radius{
UIBezierPath*path = [UIBezierPath bezierPathWithArcCenter:controlPoint radius:radiusstart Angle:startLocation endAngle:endLocation clockwise:clockwise];
return path;
}
/*
相当于绘制一个layer层(相等于贝塞尔曲线的容器,是为了绘制贝塞尔曲线路径并添加到View上,这个和贝塞尔曲线结合使用) 没有边框的
color:贝塞尔曲线所画的填充颜色
path:贝塞尔曲线
*/
-(CAShapeLayer *)creatCAShapeLayer:(UIColor*)color and:(UIBezierPath *)path{
CAShapeLayer *layer = [CAShapeLayer layer];
layer.fillColor = color.CGColor;
layer.path = path.CGPath;
return layer;
}
效果图 Simulator Screen Shot - iPhone Xʀ - 2019-07-19 at 10.34.08.png