gpt4 book ai didi

ios - 如何删除某些状态下的 UIButton 图像?

转载 作者:技术小花猫 更新时间:2023-10-29 11:02:22 24 4
gpt4 key购买 nike

我正在开发纸牌游戏。我希望卡片背面显示图像,卡片正面显示卡片内容。我已经让图像显示在背面,但我不知道如何在选择它时清除它。 (所有 run-this-code-when-it's-selected 代码都在运行,所以我知道这不是不实际改变状态的问题。)这是我的代码:

-(void)updateUI {
for (UIButton *cardButton in self.cardButtons) {
Card *card = [self.game cardAtIndex:[self.cardButtons indexOfObject:cardButton]];
[cardButton setTitle:card.contents forState:UIControlStateSelected];
[cardButton setTitle:card.contents forState:UIControlStateSelected | UIControlStateDisabled];
[cardButton setImage:[UIImage imageNamed:@"cardback.png"] forState:UIControlStateNormal];

//I've tried various permutations of the following three lines, but the image never disappears.

[cardButton setImage:nil forState:UIControlStateSelected];
[cardButton setImage:nil forState:UIControlStateSelected | UIControlStateHighlighted];
[cardButton setImage:nil forState:UIControlStateNormal];

cardButton.selected = card.faceUp;
cardButton.alpha=(card.unplayable ? 0.3:1.0);
[self.scoreLabel setText:[NSString stringWithFormat:@"Score: %d",self.game.score]];
}
}

关于我做错了什么的想法?

最佳答案

我猜你的问题是这个(来自 setImage:forState: 的 Apple 文档):

In general, if a property is not specified for a state, the default is to use the UIControlStateNormal value. If the UIControlStateNormal value is not set, then the property defaults to a system value. Therefore, at a minimum, you should set the value for the normal state.

因此,您不能简单地为某些状态取消设置图像,因为将使用默认图像。

我看到了这个问题的几个解决方案:

  1. 对要显示的状态使用透明图像而不是 nil
  2. 为您的卡片提供一个 UIImageView subview ,您将其设置为卡片背面图像。如果卡片面朝下,使用 setHidden: 显示 subview 。如果卡片正面朝上,使用 setHidden: 隐藏 subview 。我可能会在自定义按钮类中使用 – addTarget:action:forControlEvents: 方法来注册显示或隐藏卡片背面 subview 的自定义方法。

请注意,您可以通过在 subview 中显示卡片的正面,轻松地将选项 2 更进一步,然后您可以轻松地在这两个 subview 之间设置翻转动画,以便卡片按下时“翻转”。

关于ios - 如何删除某些状态下的 UIButton 图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14699064/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com