根据iPhone开发中当前标签颜色更改标签颜色

发布于 2024-08-31 13:58:18 字数 104 浏览 2 评论 0原文

我无法根据标签的当前颜色找出用于更改 UI 标签颜色的 if 语句代码。例如,如果标签颜色当前为红色并且按下了正确的按钮,我希望标签颜色更改为黑色。如果标签颜色为黑色,我希望标签颜色更改为蓝色。

I am having trouble figuring out the if statement code for changing the color of a UI label based on the current color of the label. For instance, if the label color is currently red and the correct button is pressed, I want the label color to change to black. If the label color is black I want the label color to change to blue.

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

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

发布评论

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

评论(1

故事还在继续 2024-09-07 13:58:18

理论上,您可以使用像 if ([label.backgroundColor isEqual:[UIColor blackColor]) { ... } 这样的条件,但您必须小心,因为看起来相同的两种颜色不一定通过 isEqual: 。 (例如,如果一个是灰度,另一个是 24 位 RGB)。

基于视图的视觉属性执行条件逻辑似乎是一个您可能需要重新考虑的设计决策。您将使将来更改视觉设计(例如,使用不同的颜色)而不在应用程序逻辑中引入错误变得更加困难。模型视图控制器模式鼓励我们解耦模型的状态、修改模型的逻辑以及直观地表示模型的视图。当标签在三种颜色之间循环时,颜色可能代表在三个可能值之间循环的某种底层逻辑状态。更好的选择是在基础模型中循环该状态,并让标签的颜色反映该状态。

例如,假设您的标签颜色代表剪刀石头布游戏中的武器选择:

// .h
typedef enum WeaponState {
    WeaponStateRock,
    WeaponStateScissors,
    WeaponStatePaper
} WeaponState;

// .m    
- (IBAction)weaponSelectorButtonClicked {
    if (weaponState == WeaponStateRock) {
        weaponState = WeaponStateScissors;
    }
    else if (weaponState == WeaponStateScissors) {
        weaponState = WeaponStatePaper;
    }
    else {
        weaponState = WeaponStateRock;
    }
    [self refreshView];
}

- (void)refreshView {
    if (weaponState == WeaponStateRock) {
        self.label.backgroundColor = [UIColor redColor];
    }
    else if (weaponState == WeaponStateScissors) {
        self.label.backgroundColor = [UIColor blackColor];
    }
    else {
        self.label.backgroundColor = [UIColor blueColor];
    }
}

In theory you could use a conditional like if ([label.backgroundColor isEqual:[UIColor blackColor]) { ... }, but you have to be careful because two colors that look the same may not necessarily pass isEqual:. (e.g. if one is grayscale and the other is 24 bit RGB).

Executing conditional logic based on visual attributes of your view seems like a design decision that you may want to reconsider anyway. You're making it more difficult to change your visual design in the future (for example, to use different colors) without introducing bugs in your application's logic. The Model View Controller pattern encourages us to decouple the state of our model, the logic that modifies it, and views that represent it visually. When your label cycles between three colors, the color presumably represents some underlying logical state that cycles between three possible values. A better choice would be to cycle that state within your underlying model, and let the label's color reflect that state.

For example, let's say your label's color represents a weapon selection in a rock, paper, scissors game:

// .h
typedef enum WeaponState {
    WeaponStateRock,
    WeaponStateScissors,
    WeaponStatePaper
} WeaponState;

// .m    
- (IBAction)weaponSelectorButtonClicked {
    if (weaponState == WeaponStateRock) {
        weaponState = WeaponStateScissors;
    }
    else if (weaponState == WeaponStateScissors) {
        weaponState = WeaponStatePaper;
    }
    else {
        weaponState = WeaponStateRock;
    }
    [self refreshView];
}

- (void)refreshView {
    if (weaponState == WeaponStateRock) {
        self.label.backgroundColor = [UIColor redColor];
    }
    else if (weaponState == WeaponStateScissors) {
        self.label.backgroundColor = [UIColor blackColor];
    }
    else {
        self.label.backgroundColor = [UIColor blueColor];
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文