平滑SKShapeNode上未弯曲路径的边缘

时间:2017-01-27 13:53:15

标签: swift sprite-kit cgpath skshapenode

我一直在使用Swift的内置绘图工具进行一些形状绘制,以绘制直线的简单路径。我注意到的一个异常现象是,如果你想要一条填充但其边缘没有线条笔划的路径,它会在没有抗锯齿的情况下渲染并且外观粗糙。相反,默认情况下,一条线总是平滑渲染。有一个选项可以打开抗锯齿,但这也没有区别。我可以使笔划与填充颜色相同,但这意味着边缘从定义形状的点位置向外扩展。

   //creates shape node to contain path
            let myTriangle = SKShapeNode()
    //declares path object
            let myPath = CGPathCreateMutable()
    //moves to starting point of shape
            CGPathMoveToPoint(myPath, nil, -50, 0)
    //draws lines of shape
            CGPathAddLineToPoint(myPath, nil, 0, 100 )
            CGPathAddLineToPoint(myPath, nil, 50, 0 )
            CGPathAddLineToPoint(myPath, nil, -50, 0 )
    //closes path
            CGPathCloseSubpath(myPath)

            myTriangle.antialiased = true
//adds path to shape node
            myTriangle.path = myPath
//sets fill colour and zero line width
            myTriangle.fillColor = SKColor.blueColor()
            myTriangle.lineWidth = 0
//sets antialiasing
            myTriangle.antialiased = true
//sets position in parent object and adds node
            myTriangle.position = CGPointMake(600, 600)
            self.addChild(myTriangle)

有什么想法吗?

非常感谢, 千瓦

1 个答案:

答案 0 :(得分:1)

关于抗锯齿,你可以看看这个document。 我也看到你的lineWidth等于0,根据这份文件没有帮助。

要改善路径的外观,您还可以尝试:

yourPath.lineCap = CGLineCap(rawValue: 1)!

其中,关注来源:

/* Line cap styles. */
public enum CGLineCap : Int32 {   
    case butt
    case round
    case square
}

Swift 3

编写的示例代码
        let myTriangle = SKShapeNode()
        let myPath = CGMutablePath()
        myPath.move(to: CGPoint(x:-110,y: 0))
        myPath.addLine(to: CGPoint(x:0,y: 290))
        myPath.addLine(to: CGPoint(x:260,y: 0))
        myPath.addLine(to: CGPoint(x:-110,y: 0))
        myPath.closeSubpath()
        myTriangle.path = myPath
        myTriangle.fillColor = SKColor.white
        myTriangle.lineWidth = 2
        myTriangle.lineCap = CGLineCap(rawValue: 1)!
        myTriangle.strokeColor = SKColor.white
        myTriangle.isAntialiased = true
        myTriangle.position = CGPoint(x:self.frame.midX,y:self.frame.midY)
        self.addChild(myTriangle)

此代码的输出lineWidth 2和lineCap已经增值..):

enter image description here

您的输出lineWidth等于0):

enter image description here

相关问题