I am drawing a circle in the -drawRect: method of my UIView using the standard CGContextFillEllipseInRect() code. However, I would like to slightly pulse (make larger and smaller) and change the intensity of the color fill with an animation. For example, if the circle is filled with red I would like to pulse the circle and make the red slightly lighter and darker in-time with the pulsing action. Not having much experience with Core Animation I am a bit lost about how to do this, so any help would be greatly appreciated.
I am drawing a circle in the -drawRect: method of my UIView using the
Share
This is much simpler if you don’t draw the circle in
drawRect:. Instead, set up your view to use aCAShapeLayer, like this:The system sends
layoutSubviewsto your view whenever it changes size (including when it first appears). We overridelayoutSubviewsto set up the shape and animate it:Here’s how we set the layer’s path (which determines its shape) and the fill color for the shape:
We need to attach two animations to the layer – one for the path and one for the fill color:
Here’s how we animate the layer’s path:
Here’s how we animate the layer’s fill color:
Both of the
attach*Animationmethods use a helper method that creates a basic animation and sets it up to repeat indefinitely with autoreverse and a one second duration: