ios - 需要用按钮 Swift 生成的 Int 更改 UIImageView

标签 ios swift button uiimageview counter

我正在开发一个可以控制气候的应用程序。值发生变化,但是,我无法根据值显示不同的 (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 */

    }

最佳答案

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

关于ios - 需要用按钮 Swift 生成的 Int 更改 UIImageView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44980622/

相关文章:

swift - 如何在扩展 SequenceType 的协议(protocol)中限制 Generator.Element 的类型

html - 大绿色按钮

javascript - 如何通过单击文本打开下拉按钮?

Django Crispy Forms - 通过助手添加按钮

ios - 如何在表格行上实现领先的滑动手势?

ios - 估计应用程序的商店大小

ios - swift - iOS 9 : How do i present UIViewController with custom size in iOS 9?

ios - NSArray 和字节顺序

ios - 为什么xcode单 View 模板不需要设置rootviewcontroller?

swift - 在 Windows 10 上的 Ubuntu 上的 bash 上安装 swift 4