在通常情况下,何时在UIView动画上使用Core Animation是合适的 [英] When is it appropriate to use Core Animation over UIView animation in common cases

查看:110
本文介绍了在通常情况下,何时在UIView动画上使用Core Animation是合适的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这与许多小错误有关,这些小错误通常被一个人认为是次要的,而被另一个人认为是主要的。

This is related to a lot of little bugs that might stereotypically be considered minor by one person, but major by another.

我越来越注意到了,就是当使用所有样式的 UIView animateWithDuration:时,它实际上不必要地修改了东西(例如我的视图的多个属性),以进行简单的隐藏/显示样式动画等。 ..

What I've noticed more and more, is that when using all flavors a UIView animateWithDuration:, it actually modifies things unnecessarily, such multiple properties of my views', to do a simple hide/reveal style animations, etc..

在某些情况下,例如UINavigationBar不能为特定的旋转过渡正确设置动画位置,或者标题视图未与状态栏一起设置动画时,情况似乎有些曲折它是框架更新,当视图的子层在其父视图的属性发生更改时会隐式地进行不同的动画处理时……

Things seem to be finicky in scenarios like a UINavigationBar not animating into position properly for a certain rotation transition, or a header view not animating along with the status bar when it's frame updates, when when a view's sublayers would implicitly animate differently when their parent view's properties change...

我已经在重新研究其中很多,并转换为CAAnimations因为它们似乎更易于管理,因为它们实际上并不修改我视图的目标属性值。

So many of these I have been revisiting, and converting to CAAnimations because they seem to be easier to manage, in they do not actually modify my views' target property values.

一个简单的示例就是使用 [查看setHidden:] ,然后对其进行动画处理或使其不可见,但是在动画运行时该视图实际上已经可见或隐藏。

A simple example being, using [view setHidden:], and then animating it or out of view, but the view is actually already visible or hidden while the animation runs.

另一个是,需要转换/旋转/缩放UINavigationController的视图,并使用CAAnimation来执行此操作,因为如果修改UINavigationController的视图及其父视图的任何transform属性值,UINavigationBar都不会移动到正确的位置。

Another one being, the need to transform/rotate/scale a UINavigationController's view, and using a CAAnimation to do it, because the UINavigationBar does not move to it's correct position if I modify the UINavigationController's view, and any of it's parent view's transform property values.

因此,对于这个问题,我来回走了,一直在寻找适合我的情况,其中一个比另一个更合适的地方,但主要是我想听听其他人如何看待这些情况,以及是否有一些苹果公司提供的见解可以使我对自己的方法感到更好。

So in conclusion to this question, I have been back and forth, and have been finding places where one is more appropriate than the other for my circumstances, but mainly, I want to hear what others think about these scenarios, and if there's is some insight into what Apple has provided that I can feel better about my approaches.

先谢谢了。

推荐答案

最终,所有UIKit样式的动画都将转换为Core Animation样式的动画;也就是说,实际上所有内容都使用Core Animation进行了动画处理。 API之间的区别主要是便利性之一:UIKit样式的动画函数更新模型值,提交动画以反映表示层随时间的变化。

At the end of the day, all UIKit-style animations are converted to Core Animation-style animations; that is, everything is actually animated using Core Animation. The difference between the APIs is mostly one of convenience: UIKit-style animation functions update the model value, committing an animation to reflect that change over time in the presentation layer.

您还必须注意要对UIKit可以设置动画的属性进行动画处理。例如,虽然您可以在 UIScrollView 上为属性设置动画,例如 contentSize contentOffset ,它们尚未得到官方支持,因此您可能需要处理副作用。

You have to also be careful that you're animating properties that UIKit says you can animate. For example, while you can technically animate properties on UIScrollView like contentSize and contentOffset, they aren't officially supported, so you have to potentially deal with side effects.

此外,框架是一种特殊情况,因为它实际上是一个派生属性,由 center transform 和<$组成c $ c>边界(除了 CALayer 上的 anchorPoint ,其中 UIView 不公开)。对视图的框架进行动画处理可能会遇到很多意想不到的问题,通常涉及旋转。 Core Animation不会出现此问题,因为 frame 并不是 CALayer 上的显式动画属性。如果遇到涉及仿射变换的怪异行为,请尝试在UIKit样式动画中使用 bounds center ,缩放,平移,旋转)。

In addition, frame is a special case because it's actually a derived property, composed of center, transform, and bounds (in addition to anchorPoint on CALayer, which UIView does not expose). Animating a view's frame can have a whole host of unintended problems, usually involving rotation. Core Animation doesn't have this problem, because frame is not an explicitly-animatable property on CALayer. Try to use bounds and center in UIKit-style animations if you're encountering weird behavior involving the affine transform (e.g., scale, translate, rotation) of that view.

的确,对UIKit中的某些视图进行动画处理可能会产生意想不到的副作用或错误,因为您还要更新模型值动画他们。另一方面,Core Animation稍微灵活一些,因为您可以对其更新模型层或表示层的方式和时间进行细粒度控制。

It's true that animating certain views in UIKit might have unintended side effects or bugs because you're updating model values in addition to animating them. Core Animation, on the other hand, is a little more flexible, because you have fine-grained control over how and when it updates either the model layer or the presentation layer.

但是我不同意UIKit不必要地修改了东西。它修改需要修改的内容,以提交您请求的动画更改以及更新其模型值。当您对诸如 frame 之类的属性进行动画处理时,这将在视图之后隐式调用该视图上的 layoutSubviews()当前的运行循环,它可以级联到其他子视图,等等。

But I would disagree that UIKit is modifying things unnecessarily. It modifies what it needs to modify in order to commit the animated changes you request as well as updating its model values. When you're animating properties such as frame, this is going to implicitly invoke layoutSubviews() on that view after the current run loop, which can cascade to other subviews and so on.

如果您希望UIKit在进行动画处理之前执行其所有布局逻辑,请调用 setNeedsLayout() layoutIfNeeded() 之前一起调用动画块。如果您希望UIKit在提交动画的同时实际上使整个子视图层次结构的更改具有动画效果,请指定 UIViewAnimationOptions.layoutSubviews 选项。这将立即在动画块内触发子视图布局,因此这些值也将被动画化。否则,由动画更改的模型值将在下一个运行循环中触发布局更新。

If you want UIKit to perform all its layout logic before you animate, then call setNeedsLayout() along with layoutIfNeeded() before you invoke an animation block. If you'd like UIKit to actually animate the entire subview hierarchy's worth of changes at the same time that your animation is committed, specify the UIViewAnimationOptions.layoutSubviews option. This will trigger subview layout immediately, within the animation block, so those values are animated as well. Otherwise, the model values that are changed by your animation will trigger layout updates on the next run loop.

通常,我很少注意到使用UIKit样式动画功能的问题。因此,作为在iOS上花费大量时间制作动画的人,我会说:

In general, I rarely notice issues using UIKit-style animation functions. So as someone who has spent an extensive amount of time animating on iOS, I would say:

在所有可能的地方使用UIKit样式的动画,因为它们真的很方便。当您遇到UIKit样式的动画问题时,或者需要特定控制图层的模型和表示值的更新方式时,应使用Core Animation样式的动画。

这篇关于在通常情况下,何时在UIView动画上使用Core Animation是合适的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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