Motivation gym5 Motivation gym5 - 5 months ago 33
Swift Question

Random image Array (xcode 6, swift2)

I do knot know how to make array for random images.I want to know how to make a random image array and please help me by writing some code.i have done with the switch case but i did not want that i want random image so anyone help?

Answer

Your question is very clear but If you are asking how to create an Array of images using swift :

var arrayOfImages = ["image1.png","image2.png","image3.jpeg","image4.jpeg"]

But if you are trying to display an image each time the user click on a Button

@IBOutlet weak var imageView:UIImageView!

    @IBAction func ButtonWasTapped(sender:UIButton){

      var random = arc4random_uniform(5)
        switch(random){

        case 0: imageView.image = UIImage(named: "image1.png")
        break

        case 1: imageView.image = UIImage(named: "image2.jpg")
            break
        case 2: imageView.image = UIImage(named: "image3.png")
            break
        case 3: imageView.image = UIImage(named: "image4.jpg")
            break
        case 4: imageView.image = UIImage(named: "image5.jpg")
            break
        default:
            break;

        }

    }