我试着用平行四边形路径填充shapelayer。
let shapeLayer = CAShapeLayer()
shapeLayer.lineWidth = 2.0
shapeLayer.fillColor = UIColor.blue.cgColor
shapeLayer.strokeColor = UIColor.black.cgColor
let path = CGMutablePath()
let points: [CGPoint] = [CGPoint(x: 10, y: 10), CGPoint(x: 100, y: 20), CGPoint(x: 100, y: 100), CGPoint(x:10, y: 80)]
for (index, point) in points.enumerated() {
path.move(to: point)
var line: CGPoint = .zero
if index == points.count - 1 {
line = points[0]
} else {
line = points[index+1]
}
path.addLine(to: line)
}
shapeLayer.path = path
layer.addSublayer(shapeLayer)
但在屏幕上我只看到平行四边形的黑色边框。我在早些时候就开始填充shapelayers,对于更难的路径来说,效果还不错。有人能建议我改正这个错误吗。
最佳答案
你不需要每次都移动点。
// ...
path.move(to: points[0])
for index in 0..<points.count {
var point: CGPoint = .zero
if index == points.count - 1 {
point = points[0]
} else {
point = points[index+1]
}
path.addLine(to: point)
}
//...
关于ios - Swift Shapelayer设置填充颜色无效,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44646677/