UIView animateWithDuration 不会对cornerRadius变化进行动画处理
我正在尝试对 UIView
实例 layer
的 cornerRadius
的变化进行动画处理,但是 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];
I'm trying to animate the change of the cornerRadius
of a UIView
instance layer
, but the variation of the cornerRadius
takes place immediately.
Here's the code:
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.
EDIT:
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];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
tl;dr: 圆角半径在
animateWithDuration:animations:
中不可设置动画。文档中有关视图动画的内容。
正如动画部分 “查看 iOS 编程指南”说
进行动画处理的完整属性列表
您可以使用较旧或较新
(您正在使用的)
如您所见,
cornerRadius
不在列表中。一些令人困惑的
UIView 动画实际上仅用于动画视图属性。令人困惑的是,您还可以在 UIView 动画块内的图层上对相同的属性进行动画处理,即框架、边界、位置、不透明度、背景颜色。因此,人们在 animateWithDuration 中看到图层动画,并相信他们可以为其中的任何视图属性设置动画。
同一部分接着说:
往下几行,您可以阅读核心动画可动画属性列表,您可以在其中看到以下内容:
因此,要对
cornerRadius
进行动画处理,您需要使用核心动画,正如您在更新的问题(和答案)中所说的那样)。我刚刚补充说试图解释为什么会这样。一些额外的说明
当人们阅读说明 animateWithDuration 是推荐的动画方式的文档时,很容易相信它正在尝试替换 CABasicAnimation、CAAnimationGroup、CAKeyframeAnimation 等,但事实并非如此。它取代了您在上面看到的
beginAnimations:context:
和commitAnimations
。tl;dr: Corner radius is not animatable in
animateWithDuration:animations:
.What the documentation says about view animations.
As the section on Animations in the "View Programming Guide for iOS" says
The full list of properties that you can animate using either the older
or the newer
(that you are using) are:
As you can see,
cornerRadius
is not in the list.Some confusion
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.The same section goes on to say:
A few lines down you can read the list of Core Animation animatable properties where you see this one:
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.Some extra clarification
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 thebeginAnimations:context:
andcommitAnimations
that you seen above.我使用此扩展来动画改变角半径:
I use this extension to animate change of corner radius:
角半径变化可以设置动画,但唯一的方法是使用 CABasicAnimation。希望这对那里的人有帮助。
A corner radius variation can be animated but the only way to do so is to use a CABasicAnimation. Hope this helps somebody out there.
从 iOS 10 开始,您实际上可以为cornerRadius设置动画:
Starting in iOS 10 you can actually animate cornerRadius:
您可以如下实现 UIView 动画: http://maniacdev.com/2013/02/ios-uiview-category-allowing-you-to-set-up-customized-animation-properties
You can implement UIView animation as here: http://maniacdev.com/2013/02/ios-uiview-category-allowing-you-to-set-up-customizable-animation-properties
看起来这不是可动画的属性之一。
请参阅此处查看完整列表:
http:// /developer.apple.com/library/ios/#documentation/WindowsViews/Conceptual/ViewPG_iPhoneOS/AnimatingViews/AnimatingViews.html
Doesn't look like that's one of the animatable properties.
See here for the full list:
http://developer.apple.com/library/ios/#documentation/WindowsViews/Conceptual/ViewPG_iPhoneOS/AnimatingViews/AnimatingViews.html
您可以通过在视图类中实现
-[actionForLayer:forKey]
来使此属性在+[UIView animateWithDuration:]
中可设置动画。有关如何操作的示例,请参阅此问题。You can make this property animatable in
+[UIView animateWithDuration:]
by implementing-[actionForLayer:forKey]
in your view class. See this question for an example of how.CornerRadius属性是可动画的
工作代码如下
//图层初始化
//动画初始化
CornerRadius property is animatable
working code is below
//layer init
// animation init