我有这些尺寸,我想用它画一个圆(显然宽度和高度都相同)
205.0
218.0
245.0
257.0
310.0
510.0
当我在 205.5 处画圆时,它很好,它是一个完美的圆。但是当我从 218 开始时,这个圆圈被切断了一点,并且每次我尝试做一个更大的圆圈时它被切断得越来越多。我的问题是,无论大小如何,我如何创建一个完美的圆圈?
这是我的代码:
func drawCircle()
{
// Get the Graphics Context
let context = UIGraphicsGetCurrentContext();
// Set the circle outerline-width
CGContextSetLineWidth(context, 5.0);
// Set the circle outerline-colour
UIColor.redColor().set()
// Create Circle
CGContextAddArc(context, (round(frame.size.width))/2, round(frame.size.height)/2, (round(frame.size.width) - 10)/2, 0.0, CGFloat(M_PI * 2.0), 1)
// Draw
CGContextStrokePath(context);
}
frame.size.width
和frame.size.height
是205、218等的数字。我将接受 Objective-C 答案。
最佳答案
如果你想画一个圆,用use CGContext.addEllipse(in:)
a.k.a. CGContextAddEllipseInRect
代替CGContextAddArc
。
// swift 3:
let context = UIGraphicsGetCurrentContext()!
context.setLineWidth(5.0)
UIColor.red.set()
context.addEllipse(in: frame) // <--
context.strokePath()
// swift 2:
let context = UIGraphicsGetCurrentContext()
CGContextSetLineWidth(context, 5.0)
UIColor.redColor().set()
CGContextAddEllipseInRect(context, frame) // <--
CGContextStrokePath(context)
关于ios - CGContextAddArc 绘制多个不同大小的圆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38842237/