1

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.

Rohorao
  • 79
  • 1
  • 6

2 Answers2

1

you can create the array of color object and pass it accordingly when you press. To apply random color on buttons just generate array of numbers between color array count and apply on accordingly.

vaibhav
  • 4,038
  • 1
  • 21
  • 51
  • I'll see if I can use your array idea but first I need to google how arrays work because I have never used arrays before. – Rohorao Sep 01 '16 at 12:43
  • great ..help yourself :) – vaibhav Sep 01 '16 at 12:47
  • "you can create the array of color object and pass it accordingly when you press." What does this part mean? – Rohorao Sep 01 '16 at 13:17
  • see those links and apply yourself towards your target [Link1](http://stackoverflow.com/questions/32364062/how-to-get-a-uicolor-from-associative-array-in-swift) and [Link2](http://stackoverflow.com/questions/31554670/shift-swift-array) or search hard on google about array .. – vaibhav Sep 01 '16 at 13:21
1

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 = [
        UIColor.blueColor(),
        UIColor.blackColor(),
        UIColor.yellowColor(),
        UIColor.redColor(),
        UIColor.orangeColor(),
        UIColor.greenColor(),
        UIColor.purpleColor(),
        UIColor.magentaColor(),
        UIColor.cyanColor()
    ]

    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() {
        super.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(_:)))
            button.addGestureRecognizer(tapRecognisor)

            buttons.append(button)
            view.addSubview(button)
        }
    }
}
Scriptable
  • 19,402
  • 5
  • 56
  • 72
  • I tested out your code and it was good. It wasn't exactly what I meant but I think I should be able to modify it to my needs. Thanks – Rohorao Sep 01 '16 at 14:47