【问题标题】:CGContextAddArc draw multiple circle at different sizesCGContextAddArc 绘制多个不同大小的圆
【发布时间】:2016-08-09 04:43:05
【问题描述】:

我有这些尺寸,我想用这些尺寸画一个圆(宽度和高度显然相同)

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.widthframe.size.height 是 205、218 等的数字。我会接受 Objective-C 的答案。

【问题讨论】:

  • “切断”是什么意思?不是一直在画圈吗?或者可能是它超出了它的容器视图的边缘或其他什么?

标签: ios objective-c swift


【解决方案1】:

如果你想画一个圆,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)

【讨论】:

    【解决方案2】:

    您在其中绘制这些圆圈的视图需要足够大,以便在其中绘制所有这些圆圈(因此 510+ x 510+)。您可以设置clipsToBounds = false 来绕过它,但这通常不是一个好主意。

    UIBezierPath 有一个简单的圆形路径初始化器。您可以使用它来绘制圆圈并指定半径。

    // Use the center of the bounds of the view so we draw circles centered in our view.
    // If you use self.center, this will be in the parent view's coordinate system and your circles could be off center.
    CGPoint centerOfBounds = CGPointMake((self.bounds.origin.x + self.bounds.size.width) / 2, (self.bounds.origin.y + self.bounds.size.height) / 2);
    
    // The startAngle, endAngle and clockwise don't matter much here since we are drawing a full circle.
    // The only requirement is the start and end angle MUST be 2*PI apart.
    UIBezierPath *two05 = [UIBezierPath bezierPathWithArcCenter:centerOfBounds radius:205.0/2, startAngle:0 endAngle:2*M_PI clockwise:false];
    UIBezierPath *two18 = [UIBezierPath bezierPathWithArcCenter:centerOfBounds radius:218.0/2, startAngle:0 endAngle:2*M_PI clockwise:false];
    UIBezierPath *two45 = [UIBezierPath bezierPathWithArcCenter:centerOfBounds radius:245.0/2, startAngle:0 endAngle:2*M_PI clockwise:false];
    UIBezierPath *two57 = [UIBezierPath bezierPathWithArcCenter:centerOfBounds radius:257.0/2, startAngle:0 endAngle:2*M_PI clockwise:false];
    UIBezierPath *three10 = [UIBezierPath bezierPathWithArcCenter:centerOfBounds radius:310.0/2, startAngle:0 endAngle:2*M_PI clockwise:false];
    UIBezierPath *five10 = [UIBezierPath bezierPathWithArcCenter:centerOfBounds radius:510.0/2, startAngle:0 endAngle:2*M_PI clockwise:false];
    
    // Set the color, line widths on the path, and call stroke on each bezier path to draw the circles
    [[UIColor blueColor] setStroke];
    two05.lineWidth = 1;
    [two05 stroke];
    // Reapeat for other paths...
    

    【讨论】:

      猜你喜欢
      • 2015-06-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多