【问题标题】:Core graphics draw concentric circles and fill the gap between them核心图形绘制同心圆并填补它们之间的空白
【发布时间】:2019-03-14 14:53:34
【问题描述】:

我需要画2个圆,用黄色填充内外圆之间的区域,即不在交叉点的区域。我如何使用核心图形来做到这一点?我试过了,但它不起作用:

   override func draw(_ rect: CGRect) {
    // Drawing code

    guard let context = UIGraphicsGetCurrentContext() else { return }

    context.setFillColor(UIColor.yellow.cgColor)

    let path = CGMutablePath()
    let radius = min(rect.width/2, rect.height/2)

    let center = CGPoint(x: rect.width/2, y: rect.height/2)
    path.addEllipse(in: CGRect(x: center.x - radius, y: center.y - radius, width: 2*radius, height: 2*radius))
    context.addPath(path)

   // context.drawPath(using: .fill)

    let path2 = CGMutablePath()
    path2.addEllipse(in: CGRect(x: center.x - radius/2, y: center.y - radius/2, width: radius, height: radius))


    context.addPath(path2)

    context.clip()

    context.drawPath(using: .fill)
}

【问题讨论】:

    标签: ios uikit core-graphics cgcontext quartz-2d


    【解决方案1】:

    您可以使用以下代码:

      let path = CGMutablePath()
        let radius = min(rect.width/2, rect.height/2)
    
        let center = CGPoint(x: rect.width/2, y: rect.height/2)
        path.addEllipse(in: CGRect(x: center.x - radius, y: center.y - radius, width: 2*radius, height: 2*radius))
    
    
        let path2 = CGMutablePath()
        path2.addEllipse(in: CGRect(x: center.x - radius/2, y: center.y - radius/2, width: radius, height: radius))
    
       path.addPath(path2)
        context.addPath(path)
    
        context.drawPath(using: .eoFill)
    
    
    //inner part
       let path3 = CGMutablePath()
        path3.addEllipse(in: CGRect(x: center.x - radius/2, y: center.y - radius/2, width: radius, height: radius))
          context.addPath(path3)
        context.setFillColor(UIColor.green.cgColor)
    context.fillPath()
    

    【讨论】:

    • 它可以,但是如果我需要用不同的颜色填充内圈,我们如何扩展它?
    • 像上面一样添加另一个路径。
    猜你喜欢
    • 2016-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-17
    相关资源
    最近更新 更多