Search code examples
iosswiftuikitcore-graphics

Resize CAShapeLayer in Swift


I am trying to achieve something like this. I am also looking for an answer in Swift. So far I have a CAShapeLayerenter image description here

For the CAShapeLayer I use this code to create it.

  let circlePath: UIBezierPath = UIBezierPath(arcCenter: CGPoint(x: 36, y: 47), radius: view.frame.width / 2, startAngle: 0, endAngle: CGFloat(M_PI * 2), clockwise: true)
  let circleShape: CAShapeLayer = CAShapeLayer()
  circleShape.path = circlePath.CGPath
  circleShape.fillColor = UIColor.clearColor().CGColor
  circleShape.lineWidth = view.frame.width - 80
  circleShape.strokeColor = Color().blue.CGColor
  circleShape.opacity = 0.87

That works fine and the look is great but when I want to have it initially be a (1,1) circle then expand to the current size it doesn't work. So I want to animate the circle from starting with a radius of 1 to a radius of 180 then be able to reverse it.


Solution

  • If you want to animate the circle from a small to a large radius then you want to create a CABasicAnimation. Make the starting value of the animation be the small circle path and the ending value be the large path. If you do a google search on CAShapeLayer Swift animation you should be able to find some examples of animating changes to the shape that's installed in a CAShapeLayer. (All the code I have on hand is written in Objective-C and I don't have time to write a new example in Swift.)