I am trying to create an open circle from UIBezierPath and turn it into an SKShapeNode, which will later be converted to an SKSpriteNode.
I had a problem where I could not figure out how to compress Sprite, while the line width did not decrease. You can see the solution here:
Resize sprite without reducing content
I ended up fixing some things and creating a new class to implement custom SKShapeNode using this function:
class ShrinkableShape: SKShapeNode {
let level: Int
let bezierPath: UIBezierPath
let scale: CGFloat
let finalLineWidth: CGFloat
let from: SKSpriteNode
let initialLineWidth = CGFloat(20)
let duration = 0.3
init(level: Int, from: SKSpriteNode) {
self.level = level
self.from = from
bezierPath = UIBezierPath(arcCenter: CGPoint(x: 0, y: 0), radius: CGFloat(((43 * level) + 140)), startAngle: CGFloat(GLKMathDegreesToRadians(-50)), endAngle: CGFloat(M_PI * 2), clockwise: false)
scale = ShrinkableShape.scaleFrom(level: level) / ShrinkableShape.scaleFrom(level: level + 1)
finalLineWidth = (initialLineWidth / scale)
super.init()
setup()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func setup() {
self.path = bezierPath.cgPath
self.strokeColor = UIColor(red:0.98, green:0.99, blue:0.99, alpha:1.00)
self.lineWidth = 20
self.position = from.position
let color = SKShapeNode(rect: self.frame)
color.fillColor = UIColor.red
color.alpha = 0.2
color.position = self.position
self.addChild(color)
}
static func scaleFrom(level: Int) -> CGFloat {
return (CGFloat((level * 43) + 140) * 2.0)
}
}
If you look at the screenshot, you may notice that the white circle is slightly different from where it should be aligned (the gray bar and the white circle should be on top of each other). This only happens when I call the scale function shown in the solution (linked above).
, - , . , ( , , )
? ? SKShapeNode? , ?
- Edit--
. - , SKShapeNode. , setup(). , , .