Search code examples
imageswiftapple-watcharc4random

Generate a new image on a button press in Swift AppleWatch


Okay so I have 4 images, and they are named in sequence 'colour1.jpg', colour2.jpg, 'colour3.jpg', 'colour4.jpg'. Each of these images will act as a button background image.

The idea is there are 4 buttons on the screen, and each time one of the buttons is pressed, the images all change randomly on each of the buttons.

I am using the arc4random_uniform to randomise the images. But it doesn't work, at the moment, if the user presses one of the buttons, a random image is chosen, but it is identical across all 4 buttons. and then it won't ever change from that image. ie. if you press the same or another button, nothing happens. It just remains on the previous image.

I am new to swift so appreciate your patience with me, but would really like to learn where I'm going wrong.

class InterfaceController: WKInterfaceController {

    @IBOutlet var blueColour: WKInterfaceButton!
    @IBOutlet var pinkColour: WKInterfaceButton!
    @IBOutlet var greenColour: WKInterfaceButton!
    @IBOutlet var yellowColour: WKInterfaceButton!


    var randomImageA1 = arc4random_uniform(4)
    var randomImageA2 = arc4random_uniform(4)
    var randomImageA3 = arc4random_uniform(4)
    var randomImageA4 = arc4random_uniform(4)

    var randomImageB1 = arc4random_uniform(4)
    var randomImageB2 = arc4random_uniform(4)
    var randomImageB3 = arc4random_uniform(4)
    var randomImageB4 = arc4random_uniform(4)

    var randomImageC1 = arc4random_uniform(4)
    var randomImageC2 = arc4random_uniform(4)
    var randomImageC3 = arc4random_uniform(4)
    var randomImageC4 = arc4random_uniform(4)

    var randomImageD1 = arc4random_uniform(4)
    var randomImageD2 = arc4random_uniform(4)
    var randomImageD3 = arc4random_uniform(4)
    var randomImageD4 = arc4random_uniform(4)

    @IBAction func onePressed() {

        blueColour.setBackgroundImageNamed("colour\(randomImageA1).jpg")
        pinkColour.setBackgroundImageNamed("colour\(randomImageA2).jpg")
        greenColour.setBackgroundImageNamed("colour\(randomImageA3).jpg")
        yellowColour.setBackgroundImageNamed("colour\(randomImageA4).jpg")

    }

    @IBAction func twoPressed() {

        blueColour.setBackgroundImageNamed("colour\(randomImageB1).jpg")
        pinkColour.setBackgroundImageNamed("colour\(randomImageB2).jpg")
        greenColour.setBackgroundImageNamed("colour\(randomImageB3).jpg")
        yellowColour.setBackgroundImageNamed("colour\(randomImageB4).jpg")

    }

    @IBAction func threePressed() {

        blueColour.setBackgroundImageNamed("colour\(randomImageC1).jpg")
        pinkColour.setBackgroundImageNamed("colour\(randomImageC2).jpg")
        greenColour.setBackgroundImageNamed("colour\(randomImageC3).jpg")
        yellowColour.setBackgroundImageNamed("colour\(randomImageC4).jpg")

    }

    @IBAction func fourPressed() {

        blueColour.setBackgroundImageNamed("colour\(randomImageD1).jpg")
        pinkColour.setBackgroundImageNamed("colour\(randomImageD2).jpg")
        greenColour.setBackgroundImageNamed("colour\(randomImageD3).jpg")
        yellowColour.setBackgroundImageNamed("colour\(randomImageD4).jpg")

    }

Solution

  • You are only calling randomImage = arc4random_uniform(5) one time. If that function randomly chooses an image for you, then you need to call it before every call to setBackgroundImage()

    Your success of randomness will also depend on how you are randomizing your image within randomImage = arc4random_uniform(5). Hard to say whether that will always return the same image or not without seeing your implementation of randomness.

    Example:

    @IBOutlet var blueColour: WKInterfaceButton!
    @IBOutlet var pinkColour: WKInterfaceButton!
    @IBOutlet var greenColour: WKInterfaceButton!
    @IBOutlet var yellowColour: WKInterfaceButton!
    
    
    var randomImage
    
    @IBAction func onePressed() {
        randomImage = arc4random_uniform(4)
        blueColour.setBackgroundImageNamed("colour\(randomImage).jpg")
        randomImage = arc4random_uniform(4)
        pinkColour.setBackgroundImageNamed("colour\(randomImage).jpg")
        randomImage = arc4random_uniform(4)
        greenColour.setBackgroundImageNamed("colour\(randomImage).jpg")
        randomImage = arc4random_uniform(4)
        yellowColour.setBackgroundImageNamed("colour\(randomImage).jpg")
    
    }