for循环项目的值不变

时间:2018-10-25 13:00:59

标签: ios for-loop swift4.2

下面是我的视图控制器。我想更改视图的背景颜色取决于即将来临的二进制数据。但是我的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)
                }
            }
 }

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

我喜欢这种语法,而不是您所拥有的。似乎在操场上为我工作。

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也不是一个好主意。