简体   繁体   English

Swift和SpriteKit:如何使用SKShapeNode实现非模糊循环计时器

[英]Swift and SpriteKit: how to implement non-fuzzy circle timer with SKShapeNode

The code below implements a circle timer with SKShapeNode where one slice of the circle vanishes as time ticks off. 下面的代码用SKShapeNode实现了一个循环计时器,随着时间的流逝,其中的一小片循环消失了。

Unfortunately, the timer is fuzzy because of limitations with SKShapeNode. 不幸的是,由于SKShapeNode的限制,计时器很模糊。

Answers like this did not address the issue, and also were old and may no longer apply. 像这样的答案不能解决问题,而且答案还很陈旧,可能不再适用。

1) Is there a way to use SKShapeNode without such fuzzy lines? 1)有没有一种方法可以使用没有此类模糊线的SKShapeNode?

2) If there is no way to fix SKShapdeNode, what are the alternatives for implementing a similar circle timer? 2)如果没有办法修复SKShapdeNode,实现类似的循环计时器有哪些替代方案?

    let timer = SKShapeNode(circleOfRadius: CGFloat(50))
    timer.fillColor = UIColor.red
    timer.strokeColor = UIColor.clear
    timer.lineWidth = 0
    timer.zRotation = CGFloat.pi / 2

    timer.path = self.circle(radius: CGFloat(50), percent: CGFloat(90))

fileprivate func circle(radius: CGFloat, percent: CGFloat) -> CGPath {
    // Adjust <percent> if less than 0
    let percent = percent < 0 ? 0 : percent

    // Generate circle path
    let start = CGFloat(0)
    let end = CGFloat.pi * 2 * percent
    let center = CGPoint.zero
    let bezierPath = UIBezierPath()
    bezierPath.move(to: center)
    bezierPath.addArc(withCenter:center, radius: radius, startAngle: start, endAngle: end, clockwise: true)
    bezierPath.addLine(to: center)

    // Return path
    return bezierPath.cgPath
}

Shaders will probably accomplish what you want, check out http://battleofbrothers.com/sirryan/understanding-shaders-in-spritekit/ 着色器可能会完成您想要的操作,请访问http://battleofbrothers.com/sirryan/understanding-shaders-in-spritekit/

Also, SKShapeNode should not be used outside of debugging according to AppleDocs, have ran into a few issues using them in the past. 同样,根据AppleDocs的建议,SKShapeNode不应该在调试之外使用,过去使用它们时遇到了一些问题。

@ElTomato mentioned setting a stroke color and line width, this will work in masking the fuzziness. @ElTomato提到设置笔触颜色和线条宽度,这将有助于掩盖模糊性。

A better solution may be to just turn off anti-aliasing with isAntialiased = false 更好的解决方案可能是仅使用isAntialiased = false关闭抗锯齿

This would depend on your graphic preference of course. 当然,这取决于您的图形偏好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM