UIView animateWithDuration不设置cornerRadius变化的动画 [英] UIView animateWithDuration doesn't animate cornerRadius variation

查看:124
本文介绍了UIView animateWithDuration不设置cornerRadius变化的动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为UIView实例layercornerRadius的变化制作动画,但是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 AnimationCABasicAnimation为该属性设置了动画.

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对象执行动画

Both UIKit and Core Animation provide support for animations, but the level of support provided by each technology varies. In UIKit, animations are performed using UIView objects

您可以使用较早版本的完整属性列表

The full list of properties that you can animate using either the older

[UIView beginAnimations:context:];
[UIView setAnimationDuration:];
// Change properties here...
[UIView commitAnimations];

或更高版本

[UIView animateWithDuration:animations:];

(您正在使用的)是:

  • 框架
  • 界线
  • 中心
  • 转换(CGAffineTransform,而不是CATransform3D)
  • alpha
  • backgroundColor
  • contentStretch

如您所见,cornerRadius不在列表中.

As you can see, cornerRadius is not in the list.

UIView动画实际上仅用于对视图属性进行动画处理.让人感到困惑的是,您还可以在UIView动画块内的图层上设置相同的属性,即帧,边界,位置,不透明度,backgroundColor.因此人们可以看到animateWithDuration内部的图层动画,并相信他们可以为其中的任何view属性设置动画.

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和视图的基础层来创建动画.由于视图和图层对象错综复杂地链接在一起,因此对视图图层的更改会影响视图本身.

In places where you want to perform more sophisticated animations, or animations not supported by the UIView class, you can use Core Animation and the view’s underlying layer to create the animation. Because view and layer objects are intricately linked together, changes to a view’s layer affect the view itself.

您可以在下面几行中看到核心动画"可设置动画的属性的列表:

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变化的动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆