两个 UILabels 之间的交叉淡入淡出

发布于 2024-10-08 15:06:29 字数 348 浏览 4 评论 0原文

假设我有两个 UILabellabelOnelabelTwo),我需要在两个标签之间交叉淡入淡出。达到这种效果的最佳方法是什么?

我尝试使用 [UIView beginAnimations:@"crossFade" context:nil]; 等淡出 labelOne 然后淡入 labelTwo 但当两个标签都处于低不透明度并且您可以看穿两者时,就会出现相当明显的间隙。我需要一个漂亮干净的交叉淡入淡出效果。我有一种感觉,我需要使用 CABasicAnimation,但我希望得到一些指导。提前致谢!

Say that I have two UILabels (labelOne and labelTwo) and I need to cross-fade between the two labels. What is the best way of achieving this effect?

I have tried using [UIView beginAnimations:@"crossFade" context:nil]; etc. to fade-out labelOne and then fade-in labelTwo but there is a fairly noticeable gap when both labels are at a low opacity and you can see through both. I need a nice clean cross-fade effect instead. I have a feeling that I will need to use CABasicAnimation but I would appreciate some guidance. Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

狼亦尘 2024-10-15 15:06:29

这将使一个标签(或任何视图)淡出,同时使另一个标签淡入。动画进行到一半时,两者的不透明度都为 50%。要单独控制 Alpha,请为每个标签使用单独的动画并延迟其中一个。

label_to_show.alpha = 0.0;
label_to_hide.alpha = 1.0;

[UIView beginAnimations:nil context:nil];

label_to_show.alpha = 1.0;
label_to_hide.alpha = 0.0;

[UIView commitAnimations];

This will fade one label (or any view) out while fading the other in. Half way through the animation, both will be at 50% opacity. To control the alpha separately, use a separate animation for each label and delay one of them.

label_to_show.alpha = 0.0;
label_to_hide.alpha = 1.0;

[UIView beginAnimations:nil context:nil];

label_to_show.alpha = 1.0;
label_to_hide.alpha = 0.0;

[UIView commitAnimations];
土豪我们做朋友吧 2024-10-15 15:06:29

您可以摆弄动画曲线,尝试找到可接受的外观,但我认为您会做得更好,只需在两个标签下方保留第三个视图,以在淡入淡出时保持背景外观。

如果您的意思是您可以同时看到两个标签不是同时淡出,而是快速淡出到标签开始不清楚的程度,请用相同淡入淡出级别的新标签替换它,然后快速淡出到完全可见性。

我说“淡入淡出”,但将背景色视图放在标签顶部并继续使该视图更加不透明,切换下面的标签,然后再次将其淡入透明,看起来也非常好。

You could fiddle around with the animation curve try and find an acceptable look but I think you will do better just maintaining a third view underneath both labels to maintain the background appearance while the fades happen.

If you mean that you can see both labels at once don't fade concurrently, but fade quickly to a point at which the label begins to be unclear, replace it with the new label at the same fade level, then fade it up quickly to full visibility.

I say "fade" but it could also look very good to put a background-colored view on top of the labels and proceed to make that view more opaque, switching the labels underneath, then fading it to transparent again.

失退 2024-10-15 15:06:29

我已经实现了类似的东西并且效果很好。

[UIView transitionWithView:self duration:0.3 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
    _label1.hidden = YES;
    _label2.hidden = NO;
} completion:nil];

I've implemented something like this and it works very well.

[UIView transitionWithView:self duration:0.3 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
    _label1.hidden = YES;
    _label2.hidden = NO;
} completion:nil];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文