带 2 个按钮的分段控件

发布于 2024-12-07 07:08:26 字数 366 浏览 2 评论 0原文

我正在尝试创建一个带有 2 个圆形矩形按钮的分段控件,但不知道如何为突出显示按钮设置“forState”。

其他人说要在 IB 中设置背景图像并以编程方式修改图像

尝试过:

[buttonName setImage:[UIImage imageNamed:@"UnselectedImage.png"] forState:UIControlStateNormal];
[buttonName setImage:[UIImage imageNamed:@"SelectedImage.png"] forState:UIControlStateHighlighted];

问题是背景图像在我单击它后不再保留,有任何线索吗?

I'm trying to create a segmented control with 2 Round rect buttons but don't know how to set the 'forState' for Highlighted button.

Others said to set the background image in IB and modify the Image programmatically

Tried:

[buttonName setImage:[UIImage imageNamed:@"UnselectedImage.png"] forState:UIControlStateNormal];
[buttonName setImage:[UIImage imageNamed:@"SelectedImage.png"] forState:UIControlStateHighlighted];

The problem is the background image doesn't remanins after I've clicked on it, any clue?

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

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

发布评论

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

评论(4

对你而言 2024-12-14 07:08:26

您可以尝试以下代码:

1 个图像:

int iButtonState = 1;

if (iButtonState  == 1) {
    [buttonName setImage:[UIImage imageNamed:@"Normal.png"] forState:UIControlStateNormal];
    iButtonState = 2;
} else if (iButtonState  == 2) {
    [buttonName setImage:[UIImage imageNamed:@"Other.png"] forState:UIControlStateNormal];
    iButtonState = 1;
}

2 个图像:

int iButtonState = 1;
int iTabImageA = 1;
int iTabImageB = 1;

if (iButtonState  == 1) {
    if (iTabImageA == 1) {
        [buttonName setImage:[UIImage imageNamed:@"ImageA1.png"] forState:UIControlStateNormal];
        iTabImageA = 2;
    } else if (iTabImageA == 2) {
        [buttonName setImage:[UIImage imageNamed:@"ImageA2.png"] forState:UIControlStateNormal];
        iTabImageA = 1;
    }
    iButtonState = 2;
} else if (iButtonState  == 2) {
    if (iTabImageB == 1) {
        [buttonName setImage:[UIImage imageNamed:@"ImageB1.png"] forState:UIControlStateNormal];
        iTabImageB = 2;
    } else if (iTabImageB == 2) {
        [buttonName setImage:[UIImage imageNamed:@"ImageB2.png"] forState:UIControlStateNormal];
        iTabImageB = 1;
    }
    iButtonState = 1;
}

You could try the following code:

1 Image:

int iButtonState = 1;

if (iButtonState  == 1) {
    [buttonName setImage:[UIImage imageNamed:@"Normal.png"] forState:UIControlStateNormal];
    iButtonState = 2;
} else if (iButtonState  == 2) {
    [buttonName setImage:[UIImage imageNamed:@"Other.png"] forState:UIControlStateNormal];
    iButtonState = 1;
}

2 Images:

int iButtonState = 1;
int iTabImageA = 1;
int iTabImageB = 1;

if (iButtonState  == 1) {
    if (iTabImageA == 1) {
        [buttonName setImage:[UIImage imageNamed:@"ImageA1.png"] forState:UIControlStateNormal];
        iTabImageA = 2;
    } else if (iTabImageA == 2) {
        [buttonName setImage:[UIImage imageNamed:@"ImageA2.png"] forState:UIControlStateNormal];
        iTabImageA = 1;
    }
    iButtonState = 2;
} else if (iButtonState  == 2) {
    if (iTabImageB == 1) {
        [buttonName setImage:[UIImage imageNamed:@"ImageB1.png"] forState:UIControlStateNormal];
        iTabImageB = 2;
    } else if (iTabImageB == 2) {
        [buttonName setImage:[UIImage imageNamed:@"ImageB2.png"] forState:UIControlStateNormal];
        iTabImageB = 1;
    }
    iButtonState = 1;
}
七禾 2024-12-14 07:08:26

仅当用户触摸按钮时才使用“突出显示”状态。一旦用户从按钮上松开手指,状态就会恢复正常。

您可以尝试使用“选定”状态(未经测试但值得尝试,因为它看起来很合乎逻辑)。如果它不起作用,您可能需要通过代码自行更改按钮的“选定”状态(在按钮操作或segmentedControl委托方法中)

The "highlighted" state is used only while the user touches the button. As soon as the user release his/her finger from the button, the states goes back to normal.

You may try using the "Selected" state instead (not tested but worth trying as it seems quite logical). If it doesn't work, you may need to change the "selected" state of your buttons yourself by code (in the button action or the segmentedControl delegate methods)

橘和柠 2024-12-14 07:08:26

您需要为 UIControlStateSelected 设置所选图像,而不是 UIControlStateHighlighted

You need to set the selected image for UIControlStateSelected, rather than UIControlStateHighlighted

月亮坠入山谷 2024-12-14 07:08:26

UIButton 有点像瞬时开关。它没有“现在很热”的状态。 “突出显示”实际上是在被点击时,然后就脱离了该状态。

看来您可能需要一些外部变量来跟踪按钮的状态,并且可能还需要管理图像的旋转。

我喜欢那个形象。一旦运行起来,效果会很好。

A UIButton is kind of like a momentary switch. It doesn't have a "hot now" state. "Highlighted" is literally just while it's being tapped, and then it's out of that state.

It seems like you maybe need some external variable to track the state of your buttons, and maybe to manage the twiddling of the images, too.

I like that image. That's going to be a nice effect, once you get it running.

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