Changing cornerRadius using Core Animation

I am trying to change the corner radius of a button (OpenNoteVisible.layer) in the following way:

CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"cornerRadius"];
animation.timingFunction = [CAMediaTimingFunction     functionWithName:kCAMediaTimingFunctionLinear];
animation.fromValue = [NSNumber numberWithFloat:10.0f];
animation.toValue = [NSNumber numberWithFloat:0.0f];
animation.duration = 1.0;
[animation.layer setCornerRadius:140.0];
[OpenNoteVisible.layer addAnimation:animation forKey:@"cornerRadius"];

But this code gives an error at the line [animation.layer setCornerRadius:140.0];
I can’t understand why. I have imported Quartz core framework.

  • Control a UIView animation's progress manually instead of automatically through the time duration
  • Better quality image from UIGraphicsGetImageFromCurrentImageContext()
  • UIImageView: get transformed UIImage
  • Swift: How to enable pinch-to-zoom for CALayer?
  • CAGradientLayer bounds not changing
  • Rounded Corners only on Top of a UIView
  • Solutions Collect From Internet About “Changing cornerRadius using Core Animation”

    You’re setting the corner radius on the layer property of the animation object; this animation object doesn’t have a layer property.

    You need to set the corner radius on the layer of the thing you’re animating, in this case OpenNoteVisible. You also need to ensure the toValue of the animation object matches the value you’re setting on the layer, otherwise you’ll get odd animations.

    Your code should now be:

    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"cornerRadius"];
    animation.timingFunction = [CAMediaTimingFunction     functionWithName:kCAMediaTimingFunctionLinear];
    animation.fromValue = [NSNumber numberWithFloat:10.0f];
    animation.toValue = [NSNumber numberWithFloat:140.0f];
    animation.duration = 1.0;
    [OpenNoteVisible.layer setCornerRadius:140.0];
    [OpenNoteVisible.layer addAnimation:animation forKey:@"cornerRadius"];