简体   繁体   English

核心图形绘制同心圆并填充它们之间的间隙

[英]Core graphics draw concentric circles and fill the gap between them

I need to draw 2 circles and fill the area between inner and outer circle with yellow color, that is the area which is not in the intersection. 我需要绘制2个圆,并用黄色填充内圆和外圆之间的区域,即不在交点处的区域。 How do I do that using core graphics? 我该如何使用核心图形? I tried this but it doesn't work: 我试过了,但是没有用:

   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)
}

You can use the following code: 您可以使用以下代码:

  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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM