Search code examples
swiftswift3sprite-kituibezierpath

Centering a UIBezierPath


I have a UIBezierPath thats a rounded square. It somewhat looks like this: enter image description here

Here's my code for the shape:

let shape = SKShapeNode()
    shape.path = UIBezierPath(roundedRect: CGRect(x:(0), y: (0), width: (250), height: (400)), cornerRadius: 64).cgPath
    shape.position = CGPoint(x: 0, y: 0)
    print(shape.position)
    shape.fillColor = UIColor.white
    shape.strokeColor = UIColor.white
    shape.lineWidth = 5
    addChild(shape)

I want to center it in the middle of the screen, but using

shape.position = CGPoint(x: self.frame.width, y: self.frame.height)

doesn't work. Thanks!


Solution

  • I suggest that you center the UIBezierPath within the shape node. For example,

    let width:CGFloat = 250
    let height:CGFloat = 400
    shape.path = UIBezierPath(roundedRect: CGRect(x:-width/2, y: -height/2, width: width, height: height), cornerRadius: 64).cgPath
    

    You can center the shape in the scene by setting its position to (0, 0).