iosanimationcore-animationrounded-cornerscatransition

Animating CALayer of UIView to round corners


I'm trying to animate the rounding of the corners of my view. The cornerRadius property is listed as animatible, but I can't seem to get it to work. I actually can't get any of the other properties to animate either, but the corners are what I'm interested in. Here's my code, and it's pretty darn simple:

[CATransaction begin];
[CATransaction setValue: [NSNumber numberWithFloat: 2.0f] forKey:kCATransactionAnimationDuration];

self.myView.layer.cornerRadius = 50.0f;

[CATransaction commit];

What am I missing here guys? The corners become rounded, but it's instantaneous instead of taking 2 seconds.


Solution

  • CABasicAnimation *anim1 = [CABasicAnimation animationWithKeyPath:@"cornerRadius"];
    anim1.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
    anim1.fromValue = [NSNumber numberWithFloat:0.0f];
    anim1.toValue = [NSNumber numberWithFloat:50.0f];
    anim1.duration = 2.0;
    [self.myView.layer addAnimation:anim1 forKey:@"cornerRadius"];