我正在尝试动画化的变化cornerRadius
of a UIView
实例layer
,但是变化的cornerRadius
立即发生。
这是代码:
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;
}];
感谢所有给我建议的人。
EDIT:
我设法使用该属性来制作动画Core Animation
, 用一个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:
.
文档中有关视图动画的内容。
As the 动画部分在“查看 iOS 编程指南”中说
UIKit 和 Core Animation 都提供了对动画的支持,但是每种技术提供的支持级别有所不同。在 UIKit 中,动画是使用 UIView 对象执行的
The 完整的属性列表你可以使用旧的
[UIView beginAnimations:context:];
[UIView setAnimationDuration:];
// Change properties here...
[UIView commitAnimations];
或较新的
[UIView animateWithDuration:animations:];
(您正在使用的)是:
- frame
- bounds
- center
- 变换(CGAffineTransform,而不是 CATransform3D)
- alpha
- 背景颜色
- 内容拉伸
如你看到的,cornerRadius
不在列表中。
有些混乱
UIView 动画实际上仅用于动画视图属性。令人困惑的是,您还可以在 UIView 动画块内的图层上对相同的属性进行动画处理,即框架、边界、位置、不透明度、背景颜色。所以人们看到里面的图层动画animateWithDuration
并相信他们可以为其中的任何视图属性设置动画。
同一部分接着说:
在您想要执行更复杂的动画或 UIView 类不支持的动画的地方,您可以使用 Core Animation 和视图的底层来创建动画。由于视图和图层对象错综复杂地链接在一起,因此对视图图层的更改会影响视图本身。
往下几行,您可以阅读核心动画可动画属性列表,您可以在其中看到以下内容:
所以要制作动画cornerRadius
you need正如您在更新的问题(和答案)中已经说过的那样,使用核心动画。我刚刚补充说试图解释为什么会这样。
一些额外的说明
当人们阅读这样的文档时animateWithDuration
是推荐的动画方式,很容易让人相信它试图取代 CABasicAnimation、CAAnimationGroup、CAKeyframeAnimation 等,但事实并非如此。它取代了beginAnimations:context:
and commitAnimations
你在上面看到的。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)