在iPhone应用程序中绘制椭圆的最佳方法是什么?

时间:2022-10-22 20:15:50

I'd like to have a similar ellipse as the Mail app in my iPhone app. A screenshot just for reference is here: http://dl-client.getdropbox.com/u/57676/screenshots/ellipse.png

我想在我的iPhone应用程序中使用与Mail应用程序类似的椭圆。仅供参考的屏幕截图如下:http://dl-client.getdropbox.com/u/57676/screenshots/ellipse.png

Ultimately I'd like the ellipse with a numerical value centered in it. Is it best to use a UIImage for this task? Perhaps less overhead to draw it with Quartz? If it's done with Quartz, can anyone display a solution for drawing it?

最后,我想要一个以数值为中心的椭圆。是否最好使用UIImage执行此任务?使用Quartz绘制它的开销可能更少?如果它是用Quartz完成的,那么任何人都可以显示绘制它的解决方案吗?

1 个解决方案

#1


Ah, a rounded rectangle. That's not too hard to draw. You can use Bezier paths to get what you want. The code looks like this:

啊,圆角矩形。这不是很难画。您可以使用Bezier路径获得所需内容。代码如下所示:

CGRect rect;
CGFloat minX = CGRectGetMinX(rect), minY = CGFloatGetMinY(rect), maxX = CGFloatGetMaxX(rect), maxY = CGRectGetMaxY(rect);

CGFloat radius = 3.0; // Adjust as you like
CGContextBeginPath(context);
CGContextMoveToPoint(context, (minX + maxX) / 2.0, minY);
CGContextAddArcToPoint(context, minX, minY, minX, maxY, radius);
CGContextAddArcToPoint(context, minX, maxY, maxX, maxY, radius);
CGContextAddArcToPoint(context, maxX, maxY, maxX, minY, radius);
CGContextAddArcToPoint(context, maxX, minY, minX, minY, radius);
CGContextClosePath(context);

Now that you have a path on your graphics context, you can draw it or outline it using the CGContextDrawPath and CGContextFillPath functions.

现在您的图形上下文中有一个路径,您可以使用CGContextDrawPath和CGContextFillPath函数绘制它或勾勒它。

#1


Ah, a rounded rectangle. That's not too hard to draw. You can use Bezier paths to get what you want. The code looks like this:

啊,圆角矩形。这不是很难画。您可以使用Bezier路径获得所需内容。代码如下所示:

CGRect rect;
CGFloat minX = CGRectGetMinX(rect), minY = CGFloatGetMinY(rect), maxX = CGFloatGetMaxX(rect), maxY = CGRectGetMaxY(rect);

CGFloat radius = 3.0; // Adjust as you like
CGContextBeginPath(context);
CGContextMoveToPoint(context, (minX + maxX) / 2.0, minY);
CGContextAddArcToPoint(context, minX, minY, minX, maxY, radius);
CGContextAddArcToPoint(context, minX, maxY, maxX, maxY, radius);
CGContextAddArcToPoint(context, maxX, maxY, maxX, minY, radius);
CGContextAddArcToPoint(context, maxX, minY, minX, minY, radius);
CGContextClosePath(context);

Now that you have a path on your graphics context, you can draw it or outline it using the CGContextDrawPath and CGContextFillPath functions.

现在您的图形上下文中有一个路径,您可以使用CGContextDrawPath和CGContextFillPath函数绘制它或勾勒它。