我如何为每一行添加颜色?我希望所有的线都有不同的颜色。例如,一侧蓝色另一侧红色另一个绿色。
override func viewDidLoad() {
super.viewDidLoad()
shape = CAShapeLayer()
view.layer.addSublayer(shape)
shape.opacity = 0.5
shape.lineWidth = 2
shape.lineJoin = kCALineJoinMiter
shape.strokeColor = UIColor(hue: 0.786, saturation: 0.79, brightness: 0.53, alpha: 1.0).CGColor
shape.fillColor = UIColor(hue: 0, saturation: 0, brightness: 0, alpha: 0).CGColor
path = UIBezierPath()
path.moveToPoint(CGPointMake(150, 300))
path.addLineToPoint(CGPointMake(230, 300))
path.addLineToPoint(CGPointMake(250, 350))
path.addLineToPoint(CGPointMake(230, 400))
path.addLineToPoint(CGPointMake(150, 400))
path.addLineToPoint(CGPointMake(150, 400))
path.addLineToPoint(CGPointMake(130, 350))
path.addLineToPoint(CGPointMake(150, 300))
path.closePath()
shape.path = path.CGPath
// Do any additional setup after loading the view, typically from a nib.
}
答案 0 :(得分:3)
单个路径(单个形状图层)以单色绘制。您应该为每个所需的颜色创建一个单独的形状图层,并将要绘制的线段以相同的颜色放入每个形状图层的路径中(使用moveToPoint / lineToPoint创建不连续的线条)。
将所有形状图层作为子图层添加到要包含图形的视图中。