UIView 动画交叉淡入淡出导致 alpha 降至 1.0 以下?
我使用 UIView 动画在两个视图之间交叉淡入淡出。我注意到以下令人惊讶的事实:
如果我在同一个地方有(比如说)两个相同的视图,并且我对它们之间的交叉淡入淡出进行动画处理(例如,将一个视图的 alpha 从 0.0 动画到 1.0,同时将另一个视图的 alpha 从 1.0 动画到 0.0)其他,在同一动画中),在动画期间,可见结果在动画期间略低于不透明 - 这是一个明显的伪影,可以通过在交叉淡入淡出视图后面放置一些其他视图来验证(它在动画期间短暂可见)在再次被遮挡之前)。
我希望(使用任何动画计时曲线)完美配对的 0->1 和 1->0 alpha 过渡总能达到净 alpha 1.0,并且在这个测试情况下,我永远不会看到阿尔法有任何明显的变化,但我看到了。
知道这是怎么回事吗?我可以解决这个问题以进行“修复”,但我最感兴趣的是我在混合概念上缺少的东西。
谢谢!
I'm crossfading between two views using UIView animation. I've noticed the following surprising fact:
If I have (say) two identical views in an identical place, and I animate a cross fade between them (e.g. animate the alpha from 0.0 to 1.0 on one while going from 1.0 to 0.0 on the other, in the same animation), during the animation, the visible result arcs slightly below opaque during the animation-- this is a noticeable artifact and can be verified by putting some other view behind the crossfaded views (it becomes visible briefly during the animation before being obscured again).
I would expect (using any animation timing curve) that perfectly paired 0->1 and 1->0 alpha transitions would always add up to a net alpha of 1.0, and that in this test situation, I should never see any visible change in alpha, yet I do.
Any idea what's going on here? I could hack around this for a "fix", but I'm mostly interested in what I'm missing conceptually in the blending.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
两个 alpha 值加起来为 1.0 的堆叠视图并没有达到您想象的效果。它们是相乘,而不是相加。
让我们一次取一小块。这是一个背景,亮度为 100%:
现在让我们在顶部添加另一个视图,不透明度为 50%。这意味着它让 50% 的背景透过
如果我们在顶部还有另外 50% 的视图怎么办?
另外后面50%的东西被传递了。这意味着 50% × 50% = 25% 的背景图层仍将显示出来。
现在,你真正想做什么?您希望新视图平滑地显示,越来越多的内容穿过旧视图。因此,只需堆叠两个视图,淡出顶部视图,但始终将底部保持为 100% 不透明度。否则,您将在动画过程中显示一些背景。
Two stacked views with alphas adding up to 1.0 doesn't do what you think it does. They are multiplied, not added.
Let's take it a chunk at a time. Here's a background, shining through 100%:
Now let's add another view on top, 50% opacity. That means it lets 50% of the background through
What if we have another 50% view on top?
Another 50% of the stuff behind is passed through. This means that 50% × 50% = 25% of the background layer will still be showing through.
Now, what do you really want to do? You want the new view to appear smoothly, an increasing amount passing through the old view. So just stack the two views, and fade out the top one, but leave the bottom at 100% opacity the whole time. Otherwise you'll be showing some of the background through during the animation.
这只是一个猜测,没有事实依据,但由于 alpha 表示为 CGFloat,考虑到表示浮点的难度,我建议不要尝试假设它的总和为 1.0。具有这种精度。它们加起来可能达到 0.99 左右,从而导致了这种伪影。
This is just a guess with no basis in actual fact, but since alpha is expressed as a
CGFloat
, I would advise against trying to assume anything about it adding up to 1.0 exactly, given the difficulty representing floating points with that type of precision. They likely add up to .99 or so, causing this artifact.