I have 9 buttons/squares that each have a different color. When the right color is pressed, all the buttons should switch colors. For example: if I press the red square, then the colors of the squares should change so that another square becomes red. So far I got
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
var Ctimer = NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: "colors", userInfo: nil, repeats: true)
B1.backgroundColor = UIColor.blueColor()
B2.backgroundColor = UIColor.greenColor()
B3.backgroundColor = UIColor.cyanColor()
B4.backgroundColor = UIColor.purpleColor()
B5.backgroundColor = UIColor.redColor()
B6.backgroundColor = UIColor.brownColor()
B7.backgroundColor = UIColor.yellowColor()
B8.backgroundColor = UIColor.orangeColor()
B9.backgroundColor = UIColor.magentaColor()
}
func colors() {
var red = CGFloat(drand48())
var green = CGFloat(drand48())
var blue = CGFloat(drand48())
self.view.backgroundColor = UIColor(red: red, green: green, blue: blue, alpha: 1.0)
}
func changeButtonColors(){
var rand = arc4random_uniform(10)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
B1, B2, etc are buttons. I am confused as to how I can randomize the change of colors without having to hardcode every possible alternative.
Aucun commentaire:
Enregistrer un commentaire