在 Cocoa Touch 中制作 UILabel 动画的更好方法?

发布于 2024-10-02 14:57:45 字数 423 浏览 0 评论 0原文

过去,当我想做一个从一个 UILabel 文本到另一个 UILabel 文本的漂亮淡入淡出动画时,我会加入以下内容:

[UIView beginAnimations:@"fade" context:nil];
[UIView setAnimationDuration:0.25];
myLabel.alpha = 0.0;
myLabel.text = @"Different string";
myLabel.alpha = 1.0;
[UIView commitAnimations];

正如您可能会说的那样,标签会快速淡出,更改内容,然后淡入。

我在其他应用程序中看到过一些实例,其中文本似乎非常平滑地淡出 - 而不会出现快速淡出和返回的情况。

是我一个人的问题还是有更好的方法来实现这一目标?

谢谢。 瑞奇。

In the past, when I've wanted to do a nice fade animation from one UILabel text to another, I chuck in the following:

[UIView beginAnimations:@"fade" context:nil];
[UIView setAnimationDuration:0.25];
myLabel.alpha = 0.0;
myLabel.text = @"Different string";
myLabel.alpha = 1.0;
[UIView commitAnimations];

As you can probably tell, the label does a quick fade out, change content, then fades back in.

I've seen some instances in other apps where the text just seems to fade really smoothly - without appearing to fade out and back in quickly.

Is it just me or is there a better way of achieving this?

Thanks.
Ricky.

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

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

发布评论

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

评论(1

夕色琉璃 2024-10-09 14:57:45

希望这有帮助。

    -(void)showButton:(UIButton *)button {
        CABasicAnimation *opacityAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
        opacityAnimation.fromValue = [NSNumber numberWithFloat:0.0f];
        opacityAnimation.toValue = [NSNumber numberWithFloat:1.0f];

        button.hidden = NO;

        [button.layer addAnimation:opacityAnimation 
                            forKey:@"opacity"];
    }

    -(void)hideButton:(UIButton *)button {

        CABasicAnimation *opacityAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
        opacityAnimation.fromValue = [NSNumber numberWithFloat:1.0f];
        opacityAnimation.toValue = [NSNumber numberWithFloat:0.0f];
        button.hidden = YES;

        [button.layer addAnimation:opacityAnimation 
                            forKey:@"opacity"];
    }

Hope this helps.

    -(void)showButton:(UIButton *)button {
        CABasicAnimation *opacityAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
        opacityAnimation.fromValue = [NSNumber numberWithFloat:0.0f];
        opacityAnimation.toValue = [NSNumber numberWithFloat:1.0f];

        button.hidden = NO;

        [button.layer addAnimation:opacityAnimation 
                            forKey:@"opacity"];
    }

    -(void)hideButton:(UIButton *)button {

        CABasicAnimation *opacityAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
        opacityAnimation.fromValue = [NSNumber numberWithFloat:1.0f];
        opacityAnimation.toValue = [NSNumber numberWithFloat:0.0f];
        button.hidden = YES;

        [button.layer addAnimation:opacityAnimation 
                            forKey:@"opacity"];
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文