在一侧绘制一个弧形切割的矩形

时间:2014-11-27 03:12:00

标签: ios objective-c core-graphics

我开始在iOS上使用核心图形绘制自定义形状。现在我正在尝试在一侧绘制一个带有透明弧形切割的矩形。

enter image description here

我正在尝试使用CGContextAddArcToPoint函数绘制弧线,但无法从中获取要点。还有,还有其他方法来实现这种形状吗?

1 个答案:

答案 0 :(得分:4)

// Set fill color to red
CGContextSetRGBFillColor(context, 1.0, 0.0, 0.0, 1.0);

// Move to the upper-left corner of the rect
CGContextMoveToPoint(context, CGRectGetMinX(rect), CGRectGetMinY(rect));

// Add a line to the left side of the hole
CGContextAddLineToPoint(context, CGRectGetMidX(rect) - holeRadius, CGRectGetMinY(rect));

// Add the semi-circular hole
CGContextAddArc(context, CGRectGetMidX(rect), CGRectGetMinY(rect), holeRadius, M_PI, 0, 1);

// Add a line to the top-right corner
CGContextAddLineToPoint(context, CGRectGetMaxX(rect), CGRectGetMinY(rect));

// Add the remaining sides of the rectangle
CGContextAddLineToPoint(context, CGRectGetMaxX(rect), CGRectGetMinY(rect));
CGContextAddLineToPoint(context, CGRectGetMaxX(rect), CGRectGetMaxY(rect));
CGContextAddLineToPoint(context, CGRectGetMinX(rect), CGRectGetMaxY(rect));

// Close path and fill with current fill color
CGContextClosePath(context);
CGContextFillPath(context);

Resulting image

现在,真正有趣的部分是弄清楚如何使它成为椭圆形孔而不是圆形孔。我留给读者:)

相关问题