如何使用iOS中的Bezier路径绘制直线和弯角的不同形状

时间:2015-11-26 14:49:10

标签: objective-c core-graphics core-animation uibezierpath cgpath

我需要在数组中使用带有CGPoints的Bezier路径绘制直线和曲线上的曲线。数组中提到的CGPoints指示形状的角。应该根据数组中的CGPoints(坐标)动态绘制路径。

Sample shape for reference

1 个答案:

答案 0 :(得分:0)

绘制多边形

   //// General Declarations
CGContextRef context = UIGraphicsGetCurrentContext();

//// Polygon Drawing
CGContextSaveGState(context);
CGContextTranslateCTM(context, CGRectGetMinX(frame) + 33.5, CGRectGetMinY(frame) + 226.5);
CGContextRotateCTM(context, -90 * M_PI / 180);

UIBezierPath* polygonPath = [UIBezierPath bezierPath];
[polygonPath moveToPoint: CGPointMake(99.5, 0)];
[polygonPath addLineToPoint: CGPointMake(185.67, 47.75)];
[polygonPath addLineToPoint: CGPointMake(185.67, 143.25)];
[polygonPath addLineToPoint: CGPointMake(99.5, 191)];
[polygonPath addLineToPoint: CGPointMake(13.33, 143.25)];
[polygonPath addLineToPoint: CGPointMake(13.33, 47.75)];
[polygonPath closePath];
[UIColor.grayColor setFill];
[polygonPath fill];

CGContextRestoreGState(context);

将它放在你的uiview的drawRect方法中:

这是图像:

enter image description here

弯曲多边形:

//// Bezier Drawing
UIBezierPath* bezierPath = [UIBezierPath bezierPath];
[bezierPath moveToPoint: CGPointMake(CGRectGetMinX(frame) + 59.5, CGRectGetMinY(frame) + 126.5)];
[bezierPath addLineToPoint: CGPointMake(CGRectGetMinX(frame) + 55.5, CGRectGetMinY(frame) + 116.5)];
[bezierPath addLineToPoint: CGPointMake(CGRectGetMinX(frame) + 59.5, CGRectGetMinY(frame) + 105.5)];
[bezierPath addLineToPoint: CGPointMake(CGRectGetMinX(frame) + 91.5, CGRectGetMinY(frame) + 55.5)];
[bezierPath addCurveToPoint: CGPointMake(CGRectGetMinX(frame) + 101.5, CGRectGetMinY(frame) + 44.5) controlPoint1: CGPointMake(CGRectGetMinX(frame) + 91.5, CGRectGetMinY(frame) + 55.5) controlPoint2: CGPointMake(CGRectGetMinX(frame) + 96.75, CGRectGetMinY(frame) + 48.5)];
[bezierPath addCurveToPoint: CGPointMake(CGRectGetMinX(frame) + 113.5, CGRectGetMinY(frame) + 39.5) controlPoint1: CGPointMake(CGRectGetMinX(frame) + 106.25, CGRectGetMinY(frame) + 40.5) controlPoint2: CGPointMake(CGRectGetMinX(frame) + 113.5, CGRectGetMinY(frame) + 39.5)];
[bezierPath addLineToPoint: CGPointMake(CGRectGetMinX(frame) + 188.5, CGRectGetMinY(frame) + 39.5)];
[bezierPath addCurveToPoint: CGPointMake(CGRectGetMinX(frame) + 203.5, CGRectGetMinY(frame) + 44.5) controlPoint1: CGPointMake(CGRectGetMinX(frame) + 188.5, CGRectGetMinY(frame) + 39.5) controlPoint2: CGPointMake(CGRectGetMinX(frame) + 198, CGRectGetMinY(frame) + 40.5)];
[bezierPath addCurveToPoint: CGPointMake(CGRectGetMinX(frame) + 213.5, CGRectGetMinY(frame) + 55.5) controlPoint1: CGPointMake(CGRectGetMinX(frame) + 209, CGRectGetMinY(frame) + 48.5) controlPoint2: CGPointMake(CGRectGetMinX(frame) + 213.5, CGRectGetMinY(frame) + 55.5)];
[bezierPath addLineToPoint: CGPointMake(CGRectGetMinX(frame) + 245.5, CGRectGetMinY(frame) + 105.5)];
[bezierPath addLineToPoint: CGPointMake(CGRectGetMinX(frame) + 249.5, CGRectGetMinY(frame) + 116.5)];
[bezierPath addLineToPoint: CGPointMake(CGRectGetMinX(frame) + 245.5, CGRectGetMinY(frame) + 126.5)];
[bezierPath addLineToPoint: CGPointMake(CGRectGetMinX(frame) + 213.5, CGRectGetMinY(frame) + 173.5)];
[bezierPath addCurveToPoint: CGPointMake(CGRectGetMinX(frame) + 203.5, CGRectGetMinY(frame) + 180.5) controlPoint1: CGPointMake(CGRectGetMinX(frame) + 213.5, CGRectGetMinY(frame) + 173.5) controlPoint2: CGPointMake(CGRectGetMinX(frame) + 209, CGRectGetMinY(frame) + 177.25)];
[bezierPath addCurveToPoint: CGPointMake(CGRectGetMinX(frame) + 188.5, CGRectGetMinY(frame) + 183.5) controlPoint1: CGPointMake(CGRectGetMinX(frame) + 198, CGRectGetMinY(frame) + 183.75) controlPoint2: CGPointMake(CGRectGetMinX(frame) + 188.5, CGRectGetMinY(frame) + 183.5)];
[bezierPath addLineToPoint: CGPointMake(CGRectGetMinX(frame) + 113.5, CGRectGetMinY(frame) + 183.5)];
[bezierPath addCurveToPoint: CGPointMake(CGRectGetMinX(frame) + 101.5, CGRectGetMinY(frame) + 180.5) controlPoint1: CGPointMake(CGRectGetMinX(frame) + 113.5, CGRectGetMinY(frame) + 183.5) controlPoint2: CGPointMake(CGRectGetMinX(frame) + 106.25, CGRectGetMinY(frame) + 183.75)];
[bezierPath addCurveToPoint: CGPointMake(CGRectGetMinX(frame) + 94.5, CGRectGetMinY(frame) + 173.5) controlPoint1: CGPointMake(CGRectGetMinX(frame) + 96.75, CGRectGetMinY(frame) + 177.25) controlPoint2: CGPointMake(CGRectGetMinX(frame) + 94.5, CGRectGetMinY(frame) + 173.5)];
[bezierPath addLineToPoint: CGPointMake(CGRectGetMinX(frame) + 59.5, CGRectGetMinY(frame) + 126.5)];
[UIColor.grayColor setFill];
[bezierPath fill];
[UIColor.grayColor setStroke];
bezierPath.lineWidth = 1;
[bezierPath stroke];

enter image description here

要产生与您的图像类似的结果:

//// Bezier Drawing
UIBezierPath* bezierPath = [UIBezierPath bezierPath];
[bezierPath moveToPoint: CGPointMake(CGRectGetMinX(frame) + 57.5, CGRectGetMinY(frame) + 126.5)];
[bezierPath addLineToPoint: CGPointMake(CGRectGetMinX(frame) + 55.5, CGRectGetMinY(frame) + 116.5)];
[bezierPath addLineToPoint: CGPointMake(CGRectGetMinX(frame) + 57.5, CGRectGetMinY(frame) + 105.5)];
[bezierPath addLineToPoint: CGPointMake(CGRectGetMinX(frame) + 73.5, CGRectGetMinY(frame) + 55.5)];
[bezierPath addCurveToPoint: CGPointMake(CGRectGetMinX(frame) + 78.5, CGRectGetMinY(frame) + 44.5) controlPoint1: CGPointMake(CGRectGetMinX(frame) + 73.5, CGRectGetMinY(frame) + 55.5) controlPoint2: CGPointMake(CGRectGetMinX(frame) + 76.12, CGRectGetMinY(frame) + 48.5)];
[bezierPath addCurveToPoint: CGPointMake(CGRectGetMinX(frame) + 84.5, CGRectGetMinY(frame) + 39.5) controlPoint1: CGPointMake(CGRectGetMinX(frame) + 80.88, CGRectGetMinY(frame) + 40.5) controlPoint2: CGPointMake(CGRectGetMinX(frame) + 84.5, CGRectGetMinY(frame) + 39.5)];
[bezierPath addLineToPoint: CGPointMake(CGRectGetMinX(frame) + 122, CGRectGetMinY(frame) + 39.5)];
[bezierPath addCurveToPoint: CGPointMake(CGRectGetMinX(frame) + 129.5, CGRectGetMinY(frame) + 44.5) controlPoint1: CGPointMake(CGRectGetMinX(frame) + 122, CGRectGetMinY(frame) + 39.5) controlPoint2: CGPointMake(CGRectGetMinX(frame) + 126.75, CGRectGetMinY(frame) + 40.5)];
[bezierPath addCurveToPoint: CGPointMake(CGRectGetMinX(frame) + 134.5, CGRectGetMinY(frame) + 55.5) controlPoint1: CGPointMake(CGRectGetMinX(frame) + 132.25, CGRectGetMinY(frame) + 48.5) controlPoint2: CGPointMake(CGRectGetMinX(frame) + 134.5, CGRectGetMinY(frame) + 55.5)];
[bezierPath addLineToPoint: CGPointMake(CGRectGetMinX(frame) + 150.5, CGRectGetMinY(frame) + 105.5)];
[bezierPath addLineToPoint: CGPointMake(CGRectGetMinX(frame) + 152.5, CGRectGetMinY(frame) + 116.5)];
[bezierPath addLineToPoint: CGPointMake(CGRectGetMinX(frame) + 150.5, CGRectGetMinY(frame) + 126.5)];
[bezierPath addLineToPoint: CGPointMake(CGRectGetMinX(frame) + 134.5, CGRectGetMinY(frame) + 173.5)];
[bezierPath addCurveToPoint: CGPointMake(CGRectGetMinX(frame) + 129.5, CGRectGetMinY(frame) + 180.5) controlPoint1: CGPointMake(CGRectGetMinX(frame) + 134.5, CGRectGetMinY(frame) + 173.5) controlPoint2: CGPointMake(CGRectGetMinX(frame) + 132.25, CGRectGetMinY(frame) + 177.25)];
[bezierPath addCurveToPoint: CGPointMake(CGRectGetMinX(frame) + 122, CGRectGetMinY(frame) + 183.5) controlPoint1: CGPointMake(CGRectGetMinX(frame) + 126.75, CGRectGetMinY(frame) + 183.75) controlPoint2: CGPointMake(CGRectGetMinX(frame) + 122, CGRectGetMinY(frame) + 183.5)];
[bezierPath addLineToPoint: CGPointMake(CGRectGetMinX(frame) + 84.5, CGRectGetMinY(frame) + 183.5)];
[bezierPath addCurveToPoint: CGPointMake(CGRectGetMinX(frame) + 78.5, CGRectGetMinY(frame) + 180.5) controlPoint1: CGPointMake(CGRectGetMinX(frame) + 84.5, CGRectGetMinY(frame) + 183.5) controlPoint2: CGPointMake(CGRectGetMinX(frame) + 80.88, CGRectGetMinY(frame) + 183.75)];
[bezierPath addCurveToPoint: CGPointMake(CGRectGetMinX(frame) + 75, CGRectGetMinY(frame) + 173.5) controlPoint1: CGPointMake(CGRectGetMinX(frame) + 76.12, CGRectGetMinY(frame) + 177.25) controlPoint2: CGPointMake(CGRectGetMinX(frame) + 75, CGRectGetMinY(frame) + 173.5)];
[bezierPath addLineToPoint: CGPointMake(CGRectGetMinX(frame) + 57.5, CGRectGetMinY(frame) + 126.5)];
[UIColor.grayColor setStroke];
bezierPath.lineWidth = 1;
[bezierPath stroke];

enter image description here