RohitSwift RohitSwift - 1 year ago 57
Swift Question

How do I change the colors of 9 objects after the right object is pressed?

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() {
// 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() {
// 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.

Answer Source

I'm not sure I fully understand what your trying to do, I have implemented some code below which creates 9 buttons all of a different colour and on tap assigns the tapped button a random colour out of the array, then finds the other button which has that new colour and replaces it with the previous one. So they are always unique... is that what you wanted?

Either way, the code below should help you to acheive what you need, it has an array of colors and methods of accessing them via the index or selecting random ones, gesture recognition on the buttons.

class ViewController: UIViewController {

    let spacing = 10
    let buttonCount = 9
    let buttonSize = 40

    var buttons = [UIButton]()

    let colors = [

    func selectRandomColor() -> UIColor {

        let randomIndex = Int(arc4random_uniform(UInt32(colors.count)))
        return colors[randomIndex]

    func buttonTapped(gestureRecognizer: UIGestureRecognizer) {

        let sender = gestureRecognizer.view as! UIButton
        let currentColor = sender.backgroundColor

        var randColor = selectRandomColor()

        while randColor === currentColor {
            randColor = selectRandomColor()

        sender.backgroundColor = randColor

        for button in buttons where sender !== button {
            if button.backgroundColor == randColor {
                button.backgroundColor = currentColor

    override func viewDidLoad() {
        // Do any additional setup after loading the view, typically from a nib.

        for num in 0..<buttonCount {

            let xPostion = (buttonSize * num) + (spacing * num)
            let button = UIButton(frame: CGRect(x: xPostion, y: 40, width: buttonSize, height: buttonSize))
            button.backgroundColor = colors[num]

            let tapRecognisor = UITapGestureRecognizer(target: self, action: #selector(self.buttonTapped(_:)))

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download