lascoff
lascoff

Reputation: 1331

Swift: change image multiple times

In my app I would like to display multiple images in one control and finally settling on the last image. The following code runs through fine but only the last image displays. It's like the page doesn't refresh after setting the image. Here is my code:

var num: UInt32 = 1
            let roll1: [UInt32] = [
                randRange(1,upper: 6),
                randRange(1,upper: 6),
                randRange(1,upper: 6),
                randRange(1,upper: 6),
                randRange(1,upper: 6),
                randRange(1,upper: 6),
                randRange(1,upper: 6),
                randRange(1,upper: 6)
            ]

for index in 0...7 {
                Dice1.hidden = true
                num = roll1[index]
                switch num
                {
                case 1:
                    Dice1.image = UIImage(named: "one.png")
                case 2:
                    Dice1.image = UIImage(named: "two")
                case 3:
                    Dice1.image = UIImage(named: "three")
                case 4:
                    Dice1.image = UIImage(named: "four")
                case 5:
                    Dice1.image = UIImage(named: "five")
                case 6:
                    Dice1.image = UIImage(named: "six")
                default:
                    Dice1.image = UIImage(named: "one.png")
                }
                Dice1.hidden = false
}

How do I get the images to all display?

Upvotes: 0

Views: 1118

Answers (2)

bauerMusic
bauerMusic

Reputation: 6196

I'd recommend using NSTimer for this one. It won't hold your main thread and would be more appropriate.

Upvotes: 0

diegomen
diegomen

Reputation: 1864

You can put this at the end of the code inside the for:

sleep(0.5);

Upvotes: 1

Related Questions