使用Core Animation更改cornerRadius [英] Changing cornerRadius using Core Animation
问题描述
我试图通过以下方式更改按钮的角半径(OpenNoteVisible.layer):
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"];
但是此代码在行[animation.layer setCornerRadius:140.0]上给出错误;
我无法理解为什么。我已经导入了Quartz核心框架。
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.
推荐答案
您正在动画对象的图层属性上设置角半径;此动画对象没有图层属性。
You're setting the corner radius on the layer property of the animation object; this animation object doesn't have a layer property.
您需要在要设置动画的图层上设置角半径,在本例中为 OpenNoteVisible
。您还需要确保动画对象的 toValue
与您在图层上设置的值相匹配,否则您将获得奇怪的动画。
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.
您的代码现在应该是:
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"];
这篇关于使用Core Animation更改cornerRadius的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!