首页 > 解决方案 > UIBezierPath 的 close() 函数不适用于 SKShapeNode

问题描述

close() 函数应该在子路径的第一个点和最后一个点之间创建一条线段。它在 Playground 中工作,但在我使用 UIBezierPath 创建 SKShapeNode 并将其用于 ARSKView 时不起作用。任何想法如何解决它?

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
}

在此处输入图像描述 在此处输入图像描述

标签: iossprite-kitarkitskshapenode

解决方案


我不知道为什么会这样,但很容易解决:

    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()

推荐阅读