Merge multiple CGPaths together to make one path

前端 未结 2 387
心在旅途
心在旅途 2020-12-23 15:09

I am making a complex shape using a few CGRects and Ellipses. I would like to stroke that path once it has been created. When I stroke the path it strokes each shape. Is the

2条回答
  •  旧巷少年郎
    2020-12-23 15:49

    Based on the answer of Thomas Zoechling

    For swift3

    height = self.bounds.size.height
    let thirds = self.height / 3
    
    let aPath: CGMutablePath = CGMutablePath()
    let rect = CGRect(x: (58 - 10) / 2, y: 46, width: 10, height: self.height - 58)
    aPath.addRect(rect)
    
    aPath.addEllipse(in: CGRect(x: (58 - 48) / 2, y: 0, width: 48, height: 48))
    aPath.addEllipse(in: CGRect(x: (58 - 48) / 2, y: thirds, width: 48, height: 48))
    aPath.addEllipse(in: CGRect(x: (58 - 48) / 2, y: thirds * 2, width: 48, height: 48))
    aPath.addEllipse(in: CGRect(x: 0, y: self.height, width: 48, height: 48))
    aPath.closeSubpath()
    
    let other: CGPath = aPath.copy(strokingWithWidth: 12, lineCap: .round, lineJoin: .round, miterLimit: 1.0)
    let square = CAShapeLayer()
    square.fillColor = UIColor(red: 36/255.0, green: 56/255.0, blue: 82/255.0, alpha: 1.0).cgColor
    square.path = other
    self.layer.addSublayer(square)
    
    let square2 = CAShapeLayer()
    square2.fillColor = UIColor(red: 50/255.0, green: 70/255.0, blue: 96/255.0, alpha: 1.0).cgColor
    square2.path = aPath
    self.layer.addSublayer(square2)
    

提交回复
热议问题