Change cornerRadius using Core Animation

I am trying to change the radius of the corner of a button (OpenNoteVisible.layer) as follows:

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 in the line [animation.layer setCornerRadius: 140.0]; I do not understand why. I imported the Quartz core framework.

+7
ios core-graphics core-animation
source share
2 answers

You set the radius of the corner in the layer property of the animation object; This animation object does not have layer properties.

You need to set the radius of the corner on the layer of what you are animating, in this case OpenNoteVisible . You also need to make sure that the toValue of the animation object matches the value you set on this layer, otherwise you will 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"]; 
+20
source share

Swift 4 solution is below

 import UIKit import PlaygroundSupport let view = UIView(frame: CGRect(x: 0, y: 0, width: 400, height: 400)) view.backgroundColor = #colorLiteral(red: 1, green: 0.5763723254, blue: 0, alpha: 1) PlaygroundPage.current.liveView = view UIView.animate(withDuration: 2.5, animations: { view.layer.cornerRadius = 40 }, completion: { _ in UIView.animate(withDuration: 0.5, animations: { view.layer.cornerRadius = 0 }) }) 
0
source share

All Articles