Search code examples
iossprite-kitarkitskshapenode

UIBezierPath's close() function doesn't work for SKShapeNode


close() function should creating a line segment between the first and last points in the subpath. It works in Playground but doesn't work when I create SKShapeNode with UIBezierPath and use it for ARSKView. Any ideas how to fix it?

func view(_ view: ARSKView, nodeFor anchor: ARAnchor) -> SKNode? {
    let bezierPath = UIBezierPath()
    bezierPath.move(to: CGPoint(x: 40, y: 0))
    let shapeHeight: CGFloat = 40
    bezierPath.addQuadCurve(to: CGPoint(x: 40.0, y: shapeHeight), controlPoint: CGPoint(x: 100.0, y: shapeHeight/2.0))
    bezierPath.close()

    let shape = SKShapeNode(path: bezierPath.cgPath, centered: true)
    shape.isAntialiased = false
    shape.strokeColor = .white
    shape.fillColor = .clear
    shape.lineWidth = 2.0
    return shape
}

enter image description here enter image description here


Solution

  • I don't know the reason why this comes , but it's very easy to fix:

        bezierPath.move(to: CGPoint(x: 40, y: 0))
    
        bezierPath.addLine(to:  CGPoint(x: 40.0001, y: 0)) //Add this line
    
        let shapeHeight: CGFloat = 40
        bezierPath.addQuadCurve(to: CGPoint(x: 40.0, y: shapeHeight), controlPoint: CGPoint(x: 100.0, y: shapeHeight/2.0))
        bezierPath.close()