如何在 xcode 中在选定和未选定状态之间交叉淡入淡出按钮?

发布于 2024-10-21 07:25:12 字数 273 浏览 2 评论 0原文

我有两个按钮图像:一张用于正常状态,一张用于选定状态。 当我从选定状态更改回正常状态时,我希望在几秒钟内以交叉淡入淡出效果发生这种情况。

到目前为止,我正在做的是使用两个 UIView 动画:第一个动画在选定状态下从 alpha 1.0 变为 alpha 0.5。然后我切换到正常状态并执行第二个 UIView 动画,从 alpha 0.5 到 alpha 1.0。

我对视觉效果(从选定图像突然过渡到正常图像)不满意。我还读到不应再使用 UIView。那么这里正确的方法是什么?代码示例也非常有用。

I have two images for a button: one for the normal state, and one for the selected state.
When I change from the selected state back to the normal state, I would like this to happen with a crossfade effect over a couple of seconds.

What I am doing so far is using two UIView animations: the first one goes from an alpha of 1.0 to an alpha of .5 in the selected state. Then I switch to the normal state and perform a second UIView animation going from an alpha of .5 to an alpha of 1.0.

I am not thrilled with the visual effect (abrupt transition from selected to normal image). I also read that UIView should no longer be used. So what is the right approach here? A code sample would be very useful too.

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

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

发布评论

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

评论(3

甜警司 2024-10-28 07:25:12

对我来说,这有效:

[UIView transitionWithView: button duration: 4.0 options: UIViewAnimationOptionTransitionCrossDissolve animations: ^{
    [button setSelected: !button.isSelected];
} completion: nil];

但是,当我将相同的 UIImage 设置为 UIControlStateHighlightedUIControlStateSelected 时,这才有效三个以下电话(留下一个电话对我来说不起作用):

[button setImage: img forState: UIControlStateSelected | UIControlStateHighlighted];
[button setImage: img forState: UIControlStateSelected];
[button setImage: img forState: UIControlStateHighlighted];

For me this worked:

[UIView transitionWithView: button duration: 4.0 options: UIViewAnimationOptionTransitionCrossDissolve animations: ^{
    [button setSelected: !button.isSelected];
} completion: nil];

But this only worked when I set the same UIImage to UIControlStateHighlighted and UIControlStateSelected with those three following calls (leaving one call did not work for me):

[button setImage: img forState: UIControlStateSelected | UIControlStateHighlighted];
[button setImage: img forState: UIControlStateSelected];
[button setImage: img forState: UIControlStateHighlighted];
囍笑 2024-10-28 07:25:12

下面很简单,4秒内就会从选中状态转变为未选中状态。唯一的问题是,当按钮同时移动时,过渡效果不佳。

    button.selected = TRUE;
    CATransition *transition = [CATransition animation];
    transition.duration = 4.0;
    transition.type = kCATransitionFade;
    transition.delegate = self;
    [button.layer addAnimation:transition forKey:nil];
    button.selected = TRUE;

The following is quite simple and will transition from the selected to not selected state in 4 seconds. The only problem is that the transition doesn't work well when the button is also moving at the same time.

    button.selected = TRUE;
    CATransition *transition = [CATransition animation];
    transition.duration = 4.0;
    transition.type = kCATransitionFade;
    transition.delegate = self;
    [button.layer addAnimation:transition forKey:nil];
    button.selected = TRUE;
风启觞 2024-10-28 07:25:12

我终于找到了我需要的东西:我没有将图像分配给按钮的选定和非选定状态,而是保持按钮透明,并向该按钮添加两个视图,最初的 alpha 值为 1.0 和 0.0。

当选择按钮并输入选择器中指定的方法时,我使用动画在这两个视图之间进行转换,如下所示:

    NSArray * subviewArray = [button subviews];
    [UIView animateWithDuration:2.0
                    animations:^ {
                        ((UIView *)[subviewArray objectAtIndex:0]).alpha = 0.0;
                        ((UIView *)[subviewArray objectAtIndex:1]).alpha = 1.0;
                    }
                    completion:nil];

如果按钮在转换期间移动,则此方法也适用。我希望这可以帮助其他人将来面临同样的问题!

I finally found what I needed: instead of assigning images to the selected and non-selected states of my button, I keep my button transparent and add instead two views to that button, initially with an alpha of 1.0 and 0.0.

When the button is selected and I enter the method specified in the selector, I use an animation to transition between these two views as follows:

    NSArray * subviewArray = [button subviews];
    [UIView animateWithDuration:2.0
                    animations:^ {
                        ((UIView *)[subviewArray objectAtIndex:0]).alpha = 0.0;
                        ((UIView *)[subviewArray objectAtIndex:1]).alpha = 1.0;
                    }
                    completion:nil];

This approach also works if the button is moving during the transition. I hope this helps others facing the same question in the future!

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