iOS swift : How to merge two shapes into a solid color shape using UIBezierPath?

标签 ios swift uibezierpath cashapelayer

enter image description here

嘿~~
我正在尝试绘制一个带有矩形 handle 的实心圆点。
(背景色:紫色,前色:黑色)

但是,形状都是正确的,但是当我给它上色时,形状重叠区域有一个间隙,无法用纯色填充。

我应该如何纠正它?谢谢!

var shapeLayer: CAShapeLayer!

func draw() {
    let path = UIBezierPath()

    // draw a rectangle
    path.move(to: CGPoint(x: 90, y: 0))
    path.addLine(to: CGPoint(x: 90, y: 100))
    path.addLine(to: CGPoint(x: 110, y: 100))
    path.addLine(to: CGPoint(x: 110, y: 0))
    path.addLine(to: CGPoint(x: 90, y: 0))

    // draw a circle
    path.move(to: CGPoint(x: 100, y: 100))
    path.addArc(withCenter: CGPoint(x: 100, y: 100), radius: 50, startAngle: 0, endAngle: 2 * CGFloat.pi, clockwise: true)

    // fill colors with purple and black
    self.shapeLayer.path = path.cgPath
    self.shapeLayer?.fillColor = foreColor.cgColor
    self.backgroundColor = backColor
}

最佳答案

以相反的方向绘制矩形:

// draw a rectangle
path.move(to: CGPoint(x: 90, y: 0))
path.addLine(to: CGPoint(x: 110, y: 0))
path.addLine(to: CGPoint(x: 110, y: 100))
path.addLine(to: CGPoint(x: 90, y: 100))
path.addLine(to: CGPoint(x: 90, y: 0))

non-zero winding CoreGraphics 使用的填充使用线条的方向来确定内部/外部。

或者,您可以反转圆圈的方向:
path.addArc(withCenter: CGPoint(x: 100, y: 100), radius: 50,
    startAngle: 2 * CGFloat.pi, endAngle: 0, clockwise: false)

关于iOS swift : How to merge two shapes into a solid color shape using UIBezierPath?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59596481/

相关文章:

swift - 无法在 Collection View 方法中使用performSegue

ios - 绘制bezierPath

ios - BezierPath 和掩蔽

iphone - iOS 5 在它被解雇之前持有一个方法一段时间

ios - 移除 UITableView 的边框,而不是分隔符

javascript - Swift,使用 SwiftSoup 访问多个类来解析 HTML 文件

objective-c - 创建不规则形状的框架

ios - 使用 Codable 或 ObjectMapper 映射通用 API 响应

html - iOS-如何在横向中生成PDF

iOS归档应用面临的问题