Set SKShapeNode frame to calculateAccumutedFrame

早过忘川 提交于 2019-12-22 12:33:26

问题


This issue has been happening for days: I cannot set the specific frame I need. I need my SKShapeNode frame to be the bounding box around a circle so that it is an inscribed circle to the frame.

The blue is the current frame and the red is the frame I want it to be. I am creating the SKShapeNode like this:

init(level: Int, from: SKSpriteNode, fromScene: GameScene) {
    self.level = level
    self.from = from
    self.fromScene = fromScene

    bezierPath = UIBezierPath(arcCenter: CGPoint.zero, radius: CGFloat(((25 * level)) + 50), startAngle: CGFloat(GLKMathDegreesToRadians(-50)), endAngle: CGFloat(M_PI * 2), clockwise: false)

    let top = CGFloat((level * 25) + 50)
    let bottom = CGFloat((level + 1) * 25 + 50)
    scale = top / bottom
    finalLineWidth = (initialLineWidth / scale)

    super.init()

    self.path = bezierPath.cgPath
    self.strokeColor = UIColor(red:0.98, green:0.99, blue:0.99, alpha:1.00)
    self.lineWidth = initialLineWidth

}

The frame is total wrong though. It is some random frame, you would think it would be set to calculateAcumulatedFrame. Where does it even get this wrong frame from...

So I overrode calculateAcumulatedFrame since frame is a get-only property:

override func calculateAccumulatedFrame() -> CGRect {
    let length = bezierPath.bounds.size.width + initialLineWidth
    let origin = CGPoint(x: bezierPath.bounds.origin.x - 5, y: bezierPath.bounds.origin.y - 5)
    return CGRect(origin: origin, size: CGSize(width: length, height: length))
}

And that returns the CGRect shown by the red part in the picture.

How do I get the calculateAccumulatedFrame to be applied to the frame? Nothing I do has it update the frame to calculateAccumulatedFrame. If it is not right, how else can I set the frame then?

xScale and yScale does not work at all. And by using it, the line width of the SKShapeNode is shrinked or enlarged and completely wrong. I need calculateAccumulatedFrame to work so I can specify the frame.

Any ideas?

来源:https://stackoverflow.com/questions/40477936/set-skshapenode-frame-to-calculateaccumutedframe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!