【问题标题】:assigning integer variable to image将整数变量分配给图像
【发布时间】:2014-07-26 18:19:34
【问题描述】:

所以我有一些从 0.png 到 9.png 的图像,它们将用于得分。 现在我希望根据当前分数显示图像。我可以这样做:

scoreImage = [UIImage imageNamed:[NSString stringWithFormat:@"%d.png", score]];

上面的代码意味着我必须创建无限数量的图像来覆盖分数。 有没有更有效的方法来做到这一点?每个数字仅使用 10 张图像,例如 0.png 1.png 2.png... 9.png

提前致谢

【问题讨论】:

  • 你不能做 scoreImage = [UIImage imageNamed:[NSString stringWithFormat:@"%d.png", score % 100]];?

标签: ios objective-c ios7 sprite-kit


【解决方案1】:

MOST 的有效方法是使用直接显示文本的视图对象,如 UILabel。然后您可以简单地将其字体、大小和样式设置为所需的值,并将其文本设置为您想要的值。

您可以制作只包含数字的自定义字体,然后使用它。我从未创建过自定义字体,但我知道这是可能的。

如果做不到这一点,您可以创建一个包含 4 个 UIImageView 对象的自定义 ScoreView 类。它会有一个整数属性 score,当您设置 score 值时,它会将正确的数字图像安装到它的不同组件图像视图中。

计算数字值的代码可能如下所示:

int score = 7596;
int remainder = score;

int thousandsValue = remainder / 1000;
remainder -= thousandsValue*1000;

int hundredsValue = remainder /100;
remainder -= hundredsValue*100;

int tensValue = remainder/10;
int onesValue = remainder % 10;

NSLog(
      @"For %d, thousandsValue = %d, hundredsValue = %d, tensValue = %d,  onesValue = %d",
      score,
      thousandsValue,
      hundredsValue,
      tensValue,
      onesValue
      );

然后使用这些值为您的千位、百位、十位和个位图像视图构建图像文件名。

最后,将图像加载并安装到每个 imageView 中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-14
    • 1970-01-01
    • 1970-01-01
    • 2019-10-10
    • 2019-08-18
    • 1970-01-01
    相关资源
    最近更新 更多