andreydimitro
andreydimitro

Reputation: 39

SWIFT: Why shape in viewDidLayoutSubviews is created twice

that's the code I use to create shape it have to be in viewDidLayoutSubviews because I'm centring it to view bounds which aren't loaded in viewDidLoad ,but for some reason it creates the shape twice... Can someone help me get rid of the second shape?

override func viewDidLayoutSubviews() { super.viewDidLayoutSubviews()

   let center = circleView.center
    let circularPath = UIBezierPath(arcCenter: center, radius: 100, startAngle: -CGFloat.pi / 2, endAngle: 2 * CGFloat.pi, clockwise: true)
   // create my track layer
   let trackLayer = CAShapeLayer()
   trackLayer.path = circularPath.cgPath
   trackLayer.strokeColor = UIColor.white.cgColor
   trackLayer.lineWidth = 5.5
   trackLayer.fillColor = UIColor.clear.cgColor
   trackLayer.lineCap = CAShapeLayerLineCap.round
   view.layer.addSublayer(trackLayer)

   //        let circularPath = UIBezierPath(arcCenter: center, radius: 100, startAngle: -CGFloat.pi / 2, endAngle: 2 * CGFloat.pi, clockwise: true)
   shapeLayer.path = circularPath.cgPath
   shapeLayer.strokeColor = #colorLiteral(red: 0.9254902005, green: 0.2352941185, blue: 0.1019607857, alpha: 1)
   shapeLayer.lineWidth = 5.5
   shapeLayer.fillColor = UIColor.clear.cgColor
   shapeLayer.lineCap = CAShapeLayerLineCap.round
   shapeLayer.strokeEnd = 0

   view.layer.addSublayer(shapeLayer)



}

Upvotes: 0

Views: 45

Answers (1)

andreydimitro
andreydimitro

Reputation: 39

I was able to solve it after putting let trackLayer = CAShapeLayer() outside of viewDidLayoutSubviews

Upvotes: 1

Related Questions