例:
先看效果图:
UIColor *color = [UIColor redColor] ;
[color set]; //设置颜色
//方形
UIBezierPath* square = [UIBezierPath bezierPathWithRect:CGRectMake(20, 20, 50, 50)];
//图形的宽度
square.lineWidth = 1.0;
//边框
[square stroke];
//椭圆
UIBezierPath* ellipse = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(20, 100, 150, 50)];
[ellipse fill];
//圆形
//参数: 圆心坐标 半径 开始角度 结束角度 方向是:顺、逆时针
//圆 = 2pi = 360° M_PI_4 = 圆的/8 = 45°
//这里是顺时针算角度
UIBezierPath* circular = [UIBezierPath bezierPathWithArcCenter:CGPointMake(25, 180) radius:25 startAngle:0 endAngle:M_PI_4 clockwise:NO];
[circular stroke];
/*----绘制二次贝塞尔曲线----*/
UIBezierPath* twoCurve = [UIBezierPath bezierPath];
//启示位置
[twoCurve moveToPoint:CGPointMake(20, 220)];
//终止位置 曲线方形位置
[twoCurve addQuadCurveToPoint:CGPointMake(20, 260) controlPoint:CGPointMake(120, 260)];
[twoCurve stroke];
/*----绘制三次贝塞尔曲线----*/
UIBezierPath* thridCurve = [UIBezierPath bezierPath];
//启示位置
[thridCurve moveToPoint:CGPointMake(10, 320)];
//终止位置 第一曲线方形位置 第二曲线方形位置
[thridCurve addCurveToPoint:CGPointMake(310, 320) controlPoint1:CGPointMake(110,220) controlPoint2:CGPointMake(210, 420)];
[thridCurve stroke];
/*----自定义绘图----*/ 五边形
UIBezierPath* drawPath = [UIBezierPath bezierPath];
[drawPath moveToPoint:CGPointMake(100.0, 400.0)];
[drawPath addLineToPoint:CGPointMake(200.0, 440.0)];
[drawPath addLineToPoint:CGPointMake(160, 540)];
[drawPath addLineToPoint:CGPointMake(40.0, 540)];
[drawPath addLineToPoint:CGPointMake(0.0, 440.0)];
//设置颜色
[[UIColor blackColor] setStroke];
//结束绘图
[drawPath closePath];
[drawPath stroke];
方法 原理图解:
- (void)addQuadCurveToPoint:(CGPoint)endPoint controlPoint:(CGPoint)controlPoint;
- (void)addCurveToPoint:(CGPoint)endPoint controlPoint1:(CGPoint)controlPoint1 controlPoint2:(CGPoint)controlPoint2;
- (void)addArcWithCenter:(CGPoint)center radius:(CGFloat)radius startAngle:(CGFloat)startAngle endAngle:(CGFloat)endAngle clockwise:(BOOL)clockwise NS_AVAILABLE_IOS(4_0);