代码之家  ›  专栏  ›  技术社区  ›  GS.

将UIBezierPath居中

  •  0
  • GS.  · 技术社区  · 7 年前

    我有一个圆正方形的UIBezierPath。有点像这样: enter image description here

    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)
    

    我想把它放在屏幕中央,但使用

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

    不起作用。谢谢

    1 回复  |  直到 7 年前
        1
  •  0
  •   0x141E    7 年前

    我建议您将UIBezierPath置于形状节点的中心。例如

    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
    

    通过将形状的位置设置为(0,0),可以将形状居中放置在场景中。