gpt4 book ai didi

swift - 平滑 SKShapeNode 上未描边路径的边缘

转载 作者:可可西里 更新时间:2023-11-01 02:08:29 27 4
gpt4 key购买 nike

我一直在使用 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)

有什么想法吗?

非常感谢,千瓦

最佳答案

关于抗锯齿你可以看这里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 valorized..):

enter image description here

您的输出(lineWidth 等于 0):

enter image description here

关于swift - 平滑 SKShapeNode 上未描边路径的边缘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41895571/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com