Ios UIView具有多个相同扇区的颜色边框
例如,我有一个UIView,我需要用3种颜色制作边框。 我可以这样做:Ios UIView具有多个相同扇区的颜色边框,ios,swift,uikit,calayer,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] = []
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个扇区:
但它看起来应该是:
唯一的方法是——用几种颜色创建圆形,还是用边框来实现?
谢谢 所以,我终于完成了这个任务。以下是CALayer的扩展:
func addDividedColors(colors: [UIColor], width: CGFloat = 10) {
let circlePath = UIBezierPath(ovalIn: frame)
var segments: [CAShapeLayer] = []
let segmentAngle: CGFloat = 1.0/CGFloat(colors.count)
for index in 0..<colors.count{
let circleLayer = CAShapeLayer()
circleLayer.path = circlePath.cgPath
// start angle is number of segments * the segment angle
circleLayer.strokeStart = segmentAngle * CGFloat(index)
// end angle is the start plus one segment
circleLayer.strokeEnd = circleLayer.strokeStart + segmentAngle
circleLayer.lineWidth = width
circleLayer.strokeColor = colors[index].cgColor
circleLayer.fillColor = UIColor.clear.cgColor
// add the segment to the segments array and to the view
segments.insert(circleLayer, at: index)
addSublayer(segments[index])
}
}
func addDividedColors(颜色:[UIColor],宽度:CGFloat=10){
设circlePath=UIBezierPath(卵形:帧)
变量段:[CAShapeLayer]=[]
let segmentAngle:CGFloat=1.0/CGFloat(colors.count)
对于0..中的索引,我终于完成了这项任务。下面是CALayer的扩展:
func addDividedColors(colors: [UIColor], width: CGFloat = 10) {
let circlePath = UIBezierPath(ovalIn: frame)
var segments: [CAShapeLayer] = []
let segmentAngle: CGFloat = 1.0/CGFloat(colors.count)
for index in 0..<colors.count{
let circleLayer = CAShapeLayer()
circleLayer.path = circlePath.cgPath
// start angle is number of segments * the segment angle
circleLayer.strokeStart = segmentAngle * CGFloat(index)
// end angle is the start plus one segment
circleLayer.strokeEnd = circleLayer.strokeStart + segmentAngle
circleLayer.lineWidth = width
circleLayer.strokeColor = colors[index].cgColor
circleLayer.fillColor = UIColor.clear.cgColor
// add the segment to the segments array and to the view
segments.insert(circleLayer, at: index)
addSublayer(segments[index])
}
}
func addDividedColors(颜色:[UIColor],宽度:CGFloat=10){
设circlePath=UIBezierPath(卵形:帧)
变量段:[CAShapeLayer]=[]
let segmentAngle:CGFloat=1.0/CGFloat(colors.count)
对于0中的索引。这可能有用。您能显示insertSublayer方法吗?@iPeter insertSublayer是CALayerOhh中的默认方法,那么它是您编写的CALayer
的扩展吗?是的,它是CALayer的扩展。这可能有用。您能显示insertSublayer方法吗?@iPeter insertSublayer是一个defaCALayerOhh中的ult方法,那么它是您编写的CALayer
的扩展吗?是的,它是CALayer的扩展