-1

I have a circle and I want to make it pulsate.

func drawCircle(context: CGContext) {
    let circle = CGRect(x: circleX, y: circleY, width: circleDiameter, height: circleDiameter)
    context.setFillColor(UIColor.systemPink.cgColor)
    context.addEllipse(in: circle)
}

But I just don't understand how to do it inside CGContext

errorka
  • 29
  • 9
  • You probably need to look at CoreAnimation, Does this help you: https://stackoverflow.com/a/67611170/1619193 – Shawn Frank Mar 14 '22 at 11:11
  • @ShawnFrank Yes, I found this issue. But the fact is that I need to do more at a lower level than in that question. Thanks! – errorka Mar 14 '22 at 11:15

1 Answers1

1

Here is one way you can do it using CoreAnimation:

func drawCircle(context: CGContext) {
    context.setFillColor(UIColor.systemPink.cgColor)
    
    context.addEllipse(in: CGRect(x: bounds.origin.x,
                                  y: bounds.origin.y,
                                  width: bounds.width,
                                  height: bounds.height))
    
    context.fillPath()
    
    // Using CoreAnimation
    let animation = CABasicAnimation(keyPath: "transform.scale")
    animation.repeatCount = .infinity
    animation.duration = 2
    animation.toValue = 1.5
    animation.timingFunction = CAMediaTimingFunction(name: .easeInEaseOut)
    animation.autoreverses = true
    
    layer.add(animation, forKey: nil)
}

CoreAnimation pulse animation circle Swift iOS

Shawn Frank
  • 4,381
  • 2
  • 19
  • 29