I am drawing a circle, with an initial radius of 200
self.circle = [CAShapeLayer layer];
self.circle.fillColor = nil;
self.circle.strokeColor = [UIColor blackColor].CGColor;
self.circle.lineWidth = 7;
self.circle.bounds = CGRectMake(0, 0, 2 * radius, 2 * radius);
self.circle.path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(self.radius, self.radius)
Can anyone please tell me how to animate a change to a radius of 100?
You can use a
CAAnimationGroup
to animate thebounds
andpath
of yourCAShapeLayer
:Here is a direct swift conversion of Jonathan's code should anyone need it.
This is how I ended up doing it:
Thanks to Jacob for pointing me in the right direction.
This is, I believe, the simpler and preferred way to do it:
I got this approach from the Apple Documentation here (See listing 3-2). The important things here are that you're setting the
fromValue
and also setting the final value forself.circle
'spath
tonewPath.CGPath
right after you set up the animation. An animation does not change the underlying value of the model, so in the other approach, you're not actually making a permanent change to the shape layer'spath
variable.I've used a solution like the chosen answer in the past (using
removedOnCompletion
andfillMode
etc) but I found that on certain versions of iOS they caused memory leaks, and also sometimes that approach just doesn't work for some reason.With this approach, you're creating the animation explicitly (both where it starts from and where it ends), and you're specifying the final permanent value for the path at the end (with that last line), so that there's no need to mess with not removing the animation when it finishes (which I believe is what leads to memory leaks if you do this animation a lot of times... the unremoved animations build up in memory).
Also, I removed the animation of the
bounds
because you don't need it to animate the circle. Even if the path is drawn outside the bounds of the layer, it'll still be fully shown (see the docs forCAShapeLayer
about that). If you do need to animate the bounds for some other reason, you can use the same approach (making sure to specify thefromValue
and the final value for bounds).