Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/google-sheets/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift CAShapeLayer填充颜色将保持白色_Swift_Uiview_Cashapelayer - Fatal编程技术网

Swift CAShapeLayer填充颜色将保持白色

Swift CAShapeLayer填充颜色将保持白色,swift,uiview,cashapelayer,Swift,Uiview,Cashapelayer,我可以清楚地看到我的线条,笔划颜色正确,线条画得正确,但不管怎样,填充颜色都保持白色 我已将此层添加到另一个UIView子类(自定义视图) 我还能试什么 编辑 以下是我如何创建路径: let path = UIBezierPath() var point1:CGPoint! var point2:CGPoint! var firstpoint:CGPoint! for i in 0..<curvePoints.count-1 { point1 = curvePoint

我可以清楚地看到我的线条,笔划颜色正确,线条画得正确,但不管怎样,填充颜色都保持白色

我已将此层添加到另一个
UIView
子类(自定义视图)

我还能试什么

编辑

以下是我如何创建路径:

 let path = UIBezierPath()
 var point1:CGPoint!
 var point2:CGPoint!
 var firstpoint:CGPoint!

 for i in 0..<curvePoints.count-1
 {
    point1 = curvePoints[i]
    point2 = curvePoints[i+1]

    point1.y=size!.height-point1.y
    point2.y=size!.height-point2.y


    path.move(to: point1)
    path.addLine(to: point2)

    if( i == 0 ) {firstpoint=point1}



 }

//close the path
path.move(to: point2)
path.addLine(to: CGPoint(x: frame.width, y: frame.height))
path.move(to: CGPoint(x: frame.width, y: frame.height))
path.addLine(to: firstpoint)

path.close()
let path=UIBezierPath()
var point1:CGPoint!
变量点2:CGPoint!
var firstpoint:CGPoint!

对于0中的i.问题在于
bezierPath
,开始
点在循环中每次都会移动,因此
路径关闭()
无法正确地关闭
路径,而不是开始
点。通过删除不必要的
移动
,它可以正常工作,如下所示

let path = UIBezierPath()

let curvePoints = [
    CGPoint.init(x: 60, y: 280),
    CGPoint.init(x: 100, y: 60),
    CGPoint.init(x: 150, y: 200),
    CGPoint.init(x: 220, y: 100),
    CGPoint.init(x: 300, y: 280)
]
path.move(to: curvePoints.first!)

for i in 1..<curvePoints.count {
    path.addLine(to: curvePoints[i])
}
path.close()

let shapeLayer = CAShapeLayer()
shapeLayer.path = path.cgPath
shapeLayer.fillColor = UIColor.red.cgColor
shapeLayer.strokeColor = UIColor.green.cgColor
shapeLayer.lineWidth = 3.0
shapeLayer.lineJoin = CAShapeLayerLineJoin.round // OR kCALineJoinRound
shapeLayer.lineCap = CAShapeLayerLineCap.round // OR kCALineCapRound
self.view.layer.addSublayer(shapeLayer)
let path=UIBezierPath()
设曲线点=[
CGPoint.init(x:60,y:280),
CGPoint.init(x:100,y:60),
CGPoint.init(x:150,y:200),
CGPoint.init(x:220,y:100),
CGPoint.init(x:300,y:280)
]
路径。移动(到:curvePoints.first!)

对于1中的i..我算出了,你不必每次画新线都将路径移动到新的点,它会自动移动到那里

因此,删除
path.move(to:point2)
就解决了这个问题!
谢谢你的评论。

我从来没有为现金支付者设置过框架。在苹果的文档中,“对于层,框架矩形是一个计算属性,它是从边界、锚点和位置属性中的值派生出来的。”。你能分享路径创建代码吗?是的,我再次编辑,请检查,谢谢!是的,这就是问题所在。我没有注意到你已经解决了这个问题,但我还是用可能的解决方案更新了我的答案。
let path = UIBezierPath()

let curvePoints = [
    CGPoint.init(x: 60, y: 280),
    CGPoint.init(x: 100, y: 60),
    CGPoint.init(x: 150, y: 200),
    CGPoint.init(x: 220, y: 100),
    CGPoint.init(x: 300, y: 280)
]
path.move(to: curvePoints.first!)

for i in 1..<curvePoints.count {
    path.addLine(to: curvePoints[i])
}
path.close()

let shapeLayer = CAShapeLayer()
shapeLayer.path = path.cgPath
shapeLayer.fillColor = UIColor.red.cgColor
shapeLayer.strokeColor = UIColor.green.cgColor
shapeLayer.lineWidth = 3.0
shapeLayer.lineJoin = CAShapeLayerLineJoin.round // OR kCALineJoinRound
shapeLayer.lineCap = CAShapeLayerLineCap.round // OR kCALineCapRound
self.view.layer.addSublayer(shapeLayer)