[英]How Do I Prevent Same Random Color and Random Fact From Showing Consecutively in Swift?
I have completed the Build a Simple iPhone App with Swift course at Treehouse, and now I would like to add a feature to it. 我已经完成了Treehouse的“使用Swift构建简单的iPhone应用程序”课程,现在我想为其添加功能。 I want to prevent the same background color and same interesting fact from showing consecutively. 我想防止连续显示相同的背景色和相同的有趣事实。 I have been working on this, but I just cannot seem to figure out the correct way. 我一直在努力,但是似乎无法找出正确的方法。 I tried storing a randomIndex variable in FactBook.swift and ColorWheel.swift to hold the random numbers generated to get the random fact/color. 我尝试在FactBook.swift和ColorWheel.swift中存储一个randomIndex变量,以保存生成的随机数以获取随机事实/颜色。 Then I used a do-while loop to compare that random number with the variable and generate another random number if they matched. 然后,我使用了do-while循环将随机数与变量进行比较,如果匹配则生成另一个随机数。 However, I was not able to get the scope correct in the loop. 但是,我无法在循环中获得正确的作用域。
Here is my code that I have for the project so far: 到目前为止,这是我为该项目准备的代码:
ViewController.swift ViewController.swift
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var funFactLabel: UILabel!
@IBOutlet weak var funFactButton: UIButton!
let factBook = FactBook()
let colorWheel = ColorWheel()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
funFactLabel.text = factBook.randomFact()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func showFunFact() {
var randomColor = colorWheel.randomColor()
view.backgroundColor = randomColor
funFactButton.tintColor = randomColor
funFactLabel.text = factBook.randomFact()
}
}
FactBook.swift FactBook.swift
import Foundation
struct FactBook {
var randomIndex = 0
let factsArray = [
"Ants stretch when they wake up in the morning.",
"Ostritches can run faster than horses.",
"Olympic gold medals are actually mostly made of silver.",
"You are born with 300 bones; by the time you are an adult you will have 206.",
"It takes about 8 minutes for light from the Sun to reach the Earth.",
"Some bamboo plants can grow almost a meter in just one day.",
"Some penguins can leap 2-3 meters out of the water.",
"The state of Florida is bigger than England.",
"On average, it takes 66 days to form a new habit.",
"Mammoths still walked the Earth when the Great Pyramid was being built."
]
func randomFact() -> String {
var unsignedArrayCount = UInt32(factsArray.count)
var unsignedRandomNumber = arc4random_uniform(unsignedArrayCount)
var randomNumber = Int(unsignedRandomNumber)
// verify value of randomNumber
println(randomNumber)
return factsArray[randomNumber]
}
}
ColorWheel.swift 彩色轮
import Foundation
import UIKit
struct ColorWheel {
let colorsArray = [
UIColor(red: 90/255.0, green: 187/255.0, blue: 181/255.0, alpha: 1.0), //teal color
UIColor(red: 222/255.0, green: 171/255.0, blue: 66/255.0, alpha: 1.0), //yellow color
UIColor(red: 223/255.0, green: 86/255.0, blue: 94/255.0, alpha: 1.0), //red color
UIColor(red: 239/255.0, green: 130/255.0, blue: 100/255.0, alpha: 1.0), //orange color
UIColor(red: 77/255.0, green: 75/255.0, blue: 82/255.0, alpha: 1.0), //dark color
UIColor(red: 105/255.0, green: 94/255.0, blue: 133/255.0, alpha: 1.0), //purple color
UIColor(red: 85/255.0, green: 176/255.0, blue: 112/255.0, alpha: 1.0), //green color
]
func randomColor() -> UIColor {
var unsignedArrayCount = UInt32(colorsArray.count)
var unsignedRandomNumber = arc4random_uniform(unsignedArrayCount)
var randomNumber = Int(unsignedRandomNumber)
return colorsArray[randomNumber]
}
}
Make mutable copies of your two arrays and use those mutable arrays as your working arrays. 制作两个数组的可变副本,并将这些可变数组用作工作数组。 After you select a random element in those arrays, delete that member. 在这些数组中选择随机元素后,删除该成员。 When the count goes to zero, make a new copy if you want to keep going. 当计数变为零时,如果要继续进行,请制作一个新副本。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.