我洗了我的数组。
然后我为数组中的每个索引分配一个值。
然后,我对每个索引执行一个操作。在这种情况下,将阵列中单元格的颜色变为红色。尽管为改组后的数组中的每个索引分配了不同的值,但我仍然会重复,有时同一单元连续红色保持超过一秒钟的时间。为什么会这样,几乎就像在If语句之间不断对数组进行洗牌一样?代码如下。
我已经编辑了代码以显示整个viewController。
import UIKit
class ViewController: UIViewController {
@IBOutlet var theview: UIView!
@IBOutlet weak var timerx: UILabel!
@IBOutlet weak var square1: UIImageView!
@IBOutlet weak var square2: UIImageView!
@IBOutlet weak var square3: UIImageView!
@IBOutlet weak var square4: UIImageView!
@IBOutlet weak var square5: UIImageView!
@IBOutlet weak var square6: UIImageView!
@IBOutlet weak var square7: UIImageView!
@IBOutlet weak var square8: UIImageView!
@IBOutlet weak var square9: UIImageView!
var viewArray = [UIImageView]()
var timer:Timer?
var seconds = 11
override func viewDidLoad() {
super.viewDidLoad()
timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(timeElapsed), userInfo: nil, repeats: true)
}
@objc func timeElapsed() {
seconds -= 1
timerx.text = "\(seconds)"
if seconds <= 0 {
timer?.invalidate()
}
viewArray = [square1,square2,square3,square4,square5,square6,square7,square8,square9]
func allcellsYellow(){
square1.backgroundColor = UIColor.systemYellow
square2.backgroundColor = UIColor.systemYellow
square3.backgroundColor = UIColor.systemYellow
square4.backgroundColor = UIColor.systemYellow
square5.backgroundColor = UIColor.systemYellow
square6.backgroundColor = UIColor.systemYellow
square7.backgroundColor = UIColor.systemYellow
square8.backgroundColor = UIColor.systemYellow
square9.backgroundColor = UIColor.systemYellow
}
func allcellsRed(){
square1.backgroundColor = UIColor.red
square2.backgroundColor = UIColor.red
square3.backgroundColor = UIColor.red
square4.backgroundColor = UIColor.red
square5.backgroundColor = UIColor.red
square6.backgroundColor = UIColor.red
square7.backgroundColor = UIColor.red
square8.backgroundColor = UIColor.red
square9.backgroundColor = UIColor.red
}
viewArray.shuffle()
let randomcell = (viewArray[0])
let randomcell2 = (viewArray[1])
let randomcell3 = (viewArray[2])
let randomcell4 = (viewArray[3])
let randomcell5 = (viewArray[4])
let randomcell6 = (viewArray[5])
let randomcell7 = (viewArray[6])
let randomcell8 = (viewArray[7])
let randomcell9 = (viewArray[8])
if seconds == 10 {
randomcell.backgroundColor = UIColor.red
}
if seconds < 10 {
allcellsYellow()
}
if seconds == 9 {
randomcell2.backgroundColor = UIColor.red
}
if seconds < 9 {
allcellsYellow()
}
if seconds == 8 {
randomcell3.backgroundColor = UIColor.red
}
if seconds < 8 {
allcellsYellow()
}
if seconds == 7 {
randomcell4.backgroundColor = UIColor.red
}
if seconds < 7 {
allcellsYellow()
}
if seconds == 6 {
randomcell5.backgroundColor = UIColor.red
}
if seconds < 6 {
allcellsYellow()
}
if seconds == 5 {
randomcell6.backgroundColor = UIColor.red
}
if seconds < 5 {
allcellsYellow()
}
if seconds == 4 {
randomcell7.backgroundColor = UIColor.red
}
if seconds < 4 {
allcellsYellow()
}
if seconds == 3 {
randomcell8.backgroundColor = UIColor.red
}
if seconds < 3 {
allcellsYellow()
}
if seconds == 2 {
randomcell9.backgroundColor = UIColor.red
}
if seconds < 2 {
allcellsYellow()
}
if seconds == 1 {
randomcell.backgroundColor = UIColor.red
}
if seconds < 1 {
allcellsYellow()
}
if seconds == 0 {
allcellsRed()
}
我将使这段代码与您的代码尽可能相似,以便您可以看到需要进行哪些[[structure更改。正如人们所说的那样,您可以做很多事情来使其更加简洁和易于管理。
请仅将此作为重新布置的建议,而不是一个好的解决方案!import UIKit
class ViewController: UIViewController {
@IBOutlet var theview: UIView!
@IBOutlet weak var timerx: UILabel!
@IBOutlet weak var square1: UIImageView!
@IBOutlet weak var square2: UIImageView!
@IBOutlet weak var square3: UIImageView!
@IBOutlet weak var square4: UIImageView!
@IBOutlet weak var square5: UIImageView!
@IBOutlet weak var square6: UIImageView!
@IBOutlet weak var square7: UIImageView!
@IBOutlet weak var square8: UIImageView!
@IBOutlet weak var square9: UIImageView!
var viewArray = [UIImageView]()
var timer:Timer?
var seconds = 11
override func viewDidLoad() {
super.viewDidLoad()
timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(timeElapsed), userInfo: nil, repeats: true)
viewArray = [square1,square2,square3,square4,square5,square6,square7,square8,square9]
viewArray.shuffle()
}
func allcellsYellow(){
square1.backgroundColor = UIColor.systemYellow
square2.backgroundColor = UIColor.systemYellow
square3.backgroundColor = UIColor.systemYellow
square4.backgroundColor = UIColor.systemYellow
square5.backgroundColor = UIColor.systemYellow
square6.backgroundColor = UIColor.systemYellow
square7.backgroundColor = UIColor.systemYellow
square8.backgroundColor = UIColor.systemYellow
square9.backgroundColor = UIColor.systemYellow
}
func allcellsRed(){
square1.backgroundColor = UIColor.red
square2.backgroundColor = UIColor.red
square3.backgroundColor = UIColor.red
square4.backgroundColor = UIColor.red
square5.backgroundColor = UIColor.red
square6.backgroundColor = UIColor.red
square7.backgroundColor = UIColor.red
square8.backgroundColor = UIColor.red
square9.backgroundColor = UIColor.red
}
fileprivate func updateColours() {
let randomcell = (viewArray[0])
let randomcell2 = (viewArray[1])
let randomcell3 = (viewArray[2])
let randomcell4 = (viewArray[3])
let randomcell5 = (viewArray[4])
let randomcell6 = (viewArray[5])
let randomcell7 = (viewArray[6])
let randomcell8 = (viewArray[7])
let randomcell9 = (viewArray[8])
if seconds == 10 {
randomcell.backgroundColor = UIColor.red
}
if seconds < 10 {
allcellsYellow()
}
if seconds == 9 {
randomcell2.backgroundColor = UIColor.red
}
if seconds < 9 {
allcellsYellow()
}
if seconds == 8 {
randomcell3.backgroundColor = UIColor.red
}
if seconds < 8 {
allcellsYellow()
}
if seconds == 7 {
randomcell4.backgroundColor = UIColor.red
}
if seconds < 7 {
allcellsYellow()
}
if seconds == 6 {
randomcell5.backgroundColor = UIColor.red
}
if seconds < 6 {
allcellsYellow()
}
if seconds == 5 {
randomcell6.backgroundColor = UIColor.red
}
if seconds < 5 {
allcellsYellow()
}
if seconds == 4 {
randomcell7.backgroundColor = UIColor.red
}
if seconds < 4 {
allcellsYellow()
}
if seconds == 3 {
randomcell8.backgroundColor = UIColor.red
}
if seconds < 3 {
allcellsYellow()
}
if seconds == 2 {
randomcell9.backgroundColor = UIColor.red
}
if seconds < 2 {
allcellsYellow()
}
if seconds == 1 {
randomcell.backgroundColor = UIColor.red
}
if seconds < 1 {
allcellsYellow()
}
if seconds == 0 {
allcellsRed()
}
}
@objc func timeElapsed() {
seconds -= 1
timerx.text = "\(seconds)"
print("\(seconds)")
if seconds <= 0 {
timer?.invalidate()
}
DispatchQueue.main.async {
self.updateColours()
}
}
}
class ViewController: UIViewController {
@IBOutlet var squares: [UIImageView]!
@IBOutlet weak var timeLabel: UILabel!
var timer = Timer()
var indices: [Int] = []
var seconds = 11
override func viewDidLoad() {
super.viewDidLoad()
timer = .scheduledTimer(timeInterval: 1, target: self, selector: #selector(timeElapsed), userInfo: nil, repeats: true)
indices = squares.indices.shuffled()
setAllSquares(to: .systemYellow)
}
func setAllSquares(to color: UIColor) {
squares.forEach { $0.backgroundColor = color }
}
func setSquare(at index: Int, to color: UIColor) {
squares[index].backgroundColor = color
}
@objc func timeElapsed(_ timer: Timer) {
timeLabel.text = "seconds: \(seconds)"
if seconds <= 0 { timer.invalidate() }
switch seconds {
case 2 ... 10:
setAllSquares(to: .systemYellow)
setSquare(at: indices[seconds-2], to: .systemRed)
case 1:
setAllSquares(to: .systemYellow)
case 0:
setAllSquares(to: .systemRed)
default:
break
}
seconds -= 1
}
}
Sample project