问题描述
我正在尝试对 UIView
实例 layer
的 cornerRadius
的变化进行动画处理,但是 cornerRadius 的变化
立即发生.
I'm trying to animate the change of the cornerRadius
of a UIView
instance layer
, but the variation of the cornerRadius
takes place immediately.
代码如下:
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(10, 10, 100, 100)];
view.layer.masksToBounds = YES;
view.layer.cornerRadius = 10.0;
[UIView animateWithDuration:1.0 animations:^{
[view.layer.cornerRadius = 0.0;
}];
感谢所有愿意给我建议的人.
Thanks everybody who is going to give me any tips.
我设法使用 Core Animation
和 CABasicAnimation
为这个属性设置了动画.
I managed to animate this property using Core Animation
, using a CABasicAnimation
.
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;
[viewToAnimate.layer addAnimation:animation forKey:@"cornerRadius"];
[animation.layer setCornerRadius:0.0];
推荐答案
tl;dr: 角半径在 animateWithDuration:animations:
中不可设置动画.
tl;dr: Corner radius is not animatable in animateWithDuration:animations:
.
作为动画部分 在iOS 视图编程指南"中说
As the section on Animations in the "View Programming Guide for iOS" says
UIKit 和 Core Animation 都提供对动画的支持,但每种技术提供的支持程度各不相同.在 UIKit 中,动画是使用 UIView 对象执行的
完整的属性列表,您可以使用旧的动画
[UIView beginAnimations:context:];
[UIView setAnimationDuration:];
// Change properties here...
[UIView commitAnimations];
或更新的
[UIView animateWithDuration:animations:];
(您正在使用的)是:
- 框架
- 界限
- 中心
- 转换(CGAffineTransform,而不是 CATransform3D)
- 阿尔法
- 背景颜色
- contentStretch
如您所见,cornerRadius
不在列表中.
As you can see, cornerRadius
is not in the list.
UIView 动画实际上仅用于动画视图属性.令人困惑的是,您还可以在 UIView 动画块内的图层上为相同的属性设置动画,即框架、边界、位置、不透明度、背景颜色.所以人们会在 animateWithDuration
中看到层动画,并相信他们可以为其中的任何视图属性设置动画.
UIView animations is really only meant for animating view properties. What confuses people is that you can also animate the same properties on the layer inside the UIView animation block, i.e. the frame, bounds, position, opacity, backgroundColor. So people see layer animations inside animateWithDuration
and believe that they can animate any view property in there.
同一部分继续说:
在你想要执行更复杂的动画,或者 UIView 类不支持的动画的地方,你可以使用 Core Animation 和视图的底层来创建动画.由于视图和图层对象错综复杂地链接在一起,因此对视图图层的更改会影响视图本身.
向下几行,您可以阅读 Core Animation 可动画属性列表,您可以在其中看到这一点:
A few lines down you can read the list of Core Animation animatable properties where you see this one:
- 图层的边框(包括图层的角是否圆角)
因此,要为 cornerRadius
设置动画,您需要使用 Core Animation,正如您在更新的问题(和答案)中已经说过的那样.我只是添加了试图解释为什么会这样.
So to animate the cornerRadius
you need to use Core Animation as you've already said in your updated question (and answer). I just added tried to explain why its so.
当人们阅读说 animateWithDuration
是推荐的动画方式的文档时,很容易相信它正在尝试替换 CABasicAnimation、CAAnimationGroup、CAKeyframeAnimation 等,但实际上并非如此.它取代了您在上面看到的 beginAnimations:context:
和 commitAnimations
.
When people read the documentations that says that animateWithDuration
is the recommended way of animating it is easy to believe that it is trying to replace CABasicAnimation, CAAnimationGroup, CAKeyframeAnimation, etc. but its really not. Its replacing the beginAnimations:context:
and commitAnimations
that you seen above.
这篇关于UIView animateWithDuration 不为cornerRadius 变化设置动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!