【问题标题】:Need to change UIImageView with a Int generated by a button Swift需要使用由按钮 Swift 生成的 Int 更改 UIImageView
【发布时间】:2017-07-07 22:38:14
【问题描述】:

我正在创建一个可以控制气候的应用。值会发生变化,但是,我无法根据值显示不同的(Guage)图像。请帮忙!

这是我的代码

 /* Fan Output Up */

@IBAction func FanOutputUpBtn(_ sender: Any) {

    let name = "Fan Position\(fanCounter)"
    /* Setting the name of the fan position image including the variable fanCounter */

    fanPositionImage.image = UIImage(named: "fanPosition \(fanCounter)")
    /* Setting the name of the fan position image including the variable fanCounter */

    if fanCounter == 5 {
        /* Set maximum value */
    } else {

    fanCounter += 1
        /* Increase the fan counter position by 1 */

    TestFanCounter.text = String(Int(self.fanCounter))
        /* Display the counter image */

        self.fanPositionImage.image = UIImage(named: name)
        /* Display image for fan position based on fan counter */


}

更新

现在它是随机显示的图像,但 TestFanCounter.text = String(Int(self.fanCounter)) 显示完全正常

   /* Fan Output Up */

@IBAction func FanOutputUpBtn(_ sender: Any) {

    let name = "Fan Position \(fanCounter)"
    // Setting the name of the fan position image including the variable fanCounter

    if fanCounter == 4 {
        // Set maximum value
    } else {

    fanCounter += 1
        // Increase the fan counter position by 1

    TestFanCounter.text = String(Int(self.fanCounter))
        // Display the counter image

        self.fanPositionImage.image = UIImage(named: name)
        /* Display image for fan position based on fan counter */

    }

}

【问题讨论】:

  • 你在这里设置了两次相同的图像,这是故意的吗?在第一次和第二次分配之间,图像的命名架构也会发生变化。
  • 嗨大卫,我明白了,我现在已经改变了这个,但它完全随机化了显示的图像

标签: ios swift button uiimageview counter


【解决方案1】:

您在设置名称后递增 fanCounter。当您显示计数器时,它会比您指定用于设置图像的名称时高一个。这些值应该相等吗?

【讨论】:

  • 如果将计数器移到底部,您将使用相同的 fanCounter 值设置所有值,然后再递增。
  • 成功了,非常感谢。只剩下一个问题,0 不会显示分配给它的图像,我想你不知道为什么?
  • 如果我要在黑暗中试一试,基本图像可能被命名为“Fan Position”,这段代码会产生“Fan Position 0”
  • 我将图像命名如下(风扇位置 0、风扇位置 1、风扇位置 2、风扇位置 3、风扇位置 4)。除了粉丝位置 0 之外,所有其他人都正确显示。
  • 问题已解决,我在粉丝位置 0 中漏掉了一个大写字母。再次感谢您的帮助,非常感谢。
猜你喜欢
  • 1970-01-01
  • 2013-03-18
  • 1970-01-01
  • 2011-06-08
  • 1970-01-01
  • 2021-04-05
  • 1970-01-01
  • 2011-11-12
  • 1970-01-01
相关资源
最近更新 更多