UIView边框,具有相等颜色的几种颜色

时间:2019-03-04 06:35:11

标签: ios swift uikit calayer

例如,我有一个UIView,我需要用3种颜色制作边框。 我可以这样:

func addDividedColors(colors:[UIColor], width:CGFloat = 1) {
    let gradientLayer = CAGradientLayer()
    gradientLayer.frame =  CGRect(origin: .zero, size: self.bounds.size)
    var colorsArray: [CGColor] = []
    var locationsArray: [NSNumber] = []
    for (index, color) in colors.enumerated() {
        colorsArray.append(color.cgColor)
        colorsArray.append(color.cgColor)
        locationsArray.append(NSNumber(value: 1.0 / Double(colors.count) * Double(index)))
        locationsArray.append(NSNumber(value: 1.0 / Double(colors.count) * Double(index + 1)))
    }

    gradientLayer.locations = locationsArray
    gradientLayer.colors = colorsArray

    let shape = CAShapeLayer()
    shape.lineWidth = width
    shape.path = UIBezierPath(roundedRect: self.bounds.insetBy(dx: width/2, dy: width/2), cornerRadius: self.cornerRadius).cgPath
    shape.strokeColor = UIColor.black.cgColor
    shape.fillColor = UIColor.clear.cgColor
    gradientLayer.mask = shape
    self.insertSublayer(gradientLayer, at: 0)
}

但随后我将看到有4个扇区的边界:

Output

但是它应该看起来像:

Expectation

是唯一的方法-创建具有多种颜色的圆,还是可以通过边框实现? 谢谢!

0 个答案:

没有答案