ios - CGContextAddArc 绘制多个不同大小的圆

标签 ios objective-c swift

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

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 答案。

最佳答案

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

相关文章:

ios - 带有 allowUserInteraction 的 UIView.animate 仅在动画的最后一帧启用 UIButton

ios - 快速删除 MKMap View 上的 "My Location"注释

jquery - Cordova 应用程序的 AJAX 请求返回 'status=0'

ios - 如何在不使用 seagues 到 UIPopover 的情况下获得 Storyboard 创建的 UIViewController?

swift - 为什么快速枚举中的可选项会导致无限循环?

ios - SwiftUI 中的 Admob 原生广告

ios - iPhone 导航栏标题文本颜色在 iOS 11 中的 viewWillAppear() 或 viewWillDisappear 函数中不会改变

ios - 子类化 XCTestCase 子类 : Test Navigator does not show all tests

ios - 如何摆脱有关 View 相互叠加的通知?

ios - 调用 [self.view addSubview :self.overlayView] 后 ScrollView 重置其位置的问题;