An easy way to draw a circle using CAShapeLayer

后端 未结 2 1894
梦如初夏
梦如初夏 2020-12-25 12:29

In questions like How to draw a smooth circle..., ...Draw Circle... and ...draw filled Circles the question and answer is very broad, contains lots of unnecessary steps and

2条回答
  •  失恋的感觉
    2020-12-25 12:31

    Using CAShapeLayer Class makes drawing easy... 1.Create CAShapeLayer Object 2.Create a Circular path 3.Set the path to the wanted CAShapeLayer path 4.Add the layer to your view

      let shapeLayer = CAShapeLayer()
      let center = view.center
      let circulPath = UIBezierPath(arcCenter: center, radius: 100, startAngle: 0, endAngle: 2.0 * CGFloat.pi, clockwise: true)
    
      shapeLayer.path = circulPath.cgPath 
      view.layer.addSublayer(shapeLayer)
    

    Note That ,here I draw the circle from center of the view. You can also set the fill color for your circle like bellow:

      shapeLayer.fillColor = UIColor.red.cgColor
    

    for further study you can check on CALayer.com

提交回复
热议问题