繁体   English   中英

for循环项目的值不变

[英]Value Of for loop item is not changing

下面是我的视图控制器。 我想更改视图的背景颜色取决于即将来临的二进制数据。 但是我的for循环sendingBit变量始终显示0值。 sendingBit值没有改变。

            class ViewController: UIViewController {

            @IBOutlet weak var backgroundView: UIView!

            var timer = Timer()
            var condition = "black"
            var count = 0

 var bitEmementsArray = [0,1,1,0,0,1,0,0,0,1,0,1,1,0,0,1,0,0,0,1,0,1,1,0,0,1,0,0,0,1,0,1,1,0,0,1,0,0,0,1,0,1,1,0,0,1,0,0,0,1,1,0,0,1,0,0,0,1,0,1,1,0,0,1,0,0,0,1,0,1,1,0,0,1,0,0,0,1,0,1,1,0,0,1,0,0,0,1,0,1,1,0,0,1,0,0,0,1,1,0,0,1,0,0,0,1,0,1,1,0,0,1,0,0,0,1,0,1,1,0,0,1,0,0,0,1,0,1,1,0,0,1,0,0,0,1,0,1,1,0,0,1,0,0,0,1,1,0,0,1,0,0,0,1,0,1,1,0,0,1,0,0,0,1,0,1,1,0,0,1,0,0,0,1,0,1,1,0,0,1,0,0,0,1,0,1,1,0,0,1,0,0,0,1]            var currentColor = "black"

            override func viewDidLoad() {
                super.viewDidLoad()
                navigationController?.navigationBar.isHidden = true
                timer = Timer.scheduledTimer(timeInterval: 0.99, target: self, selector: #selector(animation), userInfo: nil, repeats : true)

            }

            @objc func animation()
            {
    for sendingBit in bitEmementsArray {
                print(sendingBit)
                if currentColor == "black" && sendingBit == 0 {
                    backgroundView.backgroundColor = UIColor.gray
                    currentColor = "grey"
                    break;
                }
                else if currentColor == "black" && sendingBit == 1 {
                    backgroundView.backgroundColor = UIColor.white
                    currentColor = "white"
                    break;
                }
                else if currentColor == "grey" && sendingBit == 0 {
                    backgroundView.backgroundColor = UIColor.black
                    currentColor = "black"
                    break;
                }
                else if currentColor == "grey" && sendingBit == 1 {
                    backgroundView.backgroundColor = UIColor.white
                    currentColor = "white"
                    break;
                }
                else if currentColor == "white" && sendingBit == 0 {
                    backgroundView.backgroundColor = UIColor.black
                    currentColor = "black"
                    break;
                }
                else if currentColor == "white" && sendingBit == 1 {
                    backgroundView.backgroundColor = UIColor.gray
                    currentColor = "grey"
                    break;
                }

            }
                count += 1
                print(count)

                if count == bitEmementsArray.count + 1
                {
                    count = 0
                    timer.invalidate()
                    AudioServicesPlayAlertSound(kSystemSoundID_Vibrate)
                    dismiss(animated: false, completion: nil)
                }
            }
 }

这个问题与我要说的预定计时器无关。 Timer.scheduledTimer方法可确保您的代码在主线程(UIThread)上运行。 将代码简化为仅启动计时器和设置背景的基础知识,这表明此方法正常工作。

尝试删除计时器,看看代码在哪里失败。 它可能在您的颜色生成代码中。 一旦找到问题的确切根源,也许您可​​以启动与此相关的新线程。

我喜欢这种语法胜过您所拥有的语法。 似乎在操场上为我工作。

import UIKit

var backgroundView = UIView()
backgroundView.backgroundColor = .black

var bitEmementsArray = [0,1,1,0,0,1,0,0,0,1,0,1,1,0,0,1,0,0,0,1,0,1,1,0,0,1,0,0,0,1,0,1,1,0,0,1,0,0,0,1,0,1,1,0,0,1,0,0,0,1,1,0,0,1,0,0,0,1,0,1,1,0,0,1,0,0,0,1,0,1,1,0,0,1,0,0,0,1,0,1,1,0,0,1,0,0,0,1,0,1,1,0,0,1,0,0,0,1,1,0,0,1,0,0,0,1,0,1,1,0,0,1,0,0,0,1,0,1,1,0,0,1,0,0,0,1,0,1,1,0,0,1,0,0,0,1,0,1,1,0,0,1,0,0,0,1,1,0,0,1,0,0,0,1,0,1,1,0,0,1,0,0,0,1,0,1,1,0,0,1,0,0,0,1,0,1,1,0,0,1,0,0,0,1,0,1,1,0,0,1,0,0,0,1]

for sendingBit in bitEmementsArray {
   switch (sendingBit, backgroundView.backgroundColor) {
   case (0, UIColor.black):
      backgroundView.backgroundColor = .gray
   case (1, UIColor.gray):
      backgroundView.backgroundColor = .black
   default:
      print("default")
   }
}

最好使用更漂亮的模式匹配语法糖。 另外,实际上,直接输入变量时,字符串输入UIColor也不是一个好主意。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM