ios - 核心图形绘制同心圆并填补它们之间的空白
问题描述
我需要绘制 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)
}
解决方案
您可以使用以下代码:
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()
推荐阅读
- .net-core - 如何在 Grace IOC 中注册 IHubContext<,>
- android - 如何通过集成数据绑定android来更改我现有的代码
- sql - Tpump 与 BTEQ 导入
- python - 如何在 Pandas 中整理(融化)数据并保留所有其他列?
- javascript - 找不到“.js”文件扩展名的引擎
- regex - 解析特定模式的字符串:从一个短语中提取多个短语
- api - 自定义样式不适用于某些区域
- python - 从冻结模型创建可训练的 Tensorflow 图
- python - 我如何在某个时间戳时间运行 python 函数。没有外部软件?
- excel - 如何通过实例化一次将动态数组存储在类中