I have an array with image names let arrowArray = ["up","down","right","left"] and I placed an imageView and a button with an action in my storyboard
I want to change my image every time i press the button, Can any one help me ?
I have an array with image names let arrowArray = ["up","down","right","left"] and I placed an imageView and a button with an action in my storyboard
I want to change my image every time i press the button, Can any one help me ?
In your view controller:
var arrowIndex: Int = 0 @IBAction func buttonClicked(_ sender: UIButton) { yourImageView.image = UIImage(named: arrowArray[arrowIndex]) arrowIndex += 1 if arrayIndex == (arrowArray.count - 1) { arrayIndex = 0 } } This will display the up arrow first. You can change arrowIndex's initial value to display different arrows first.
Thanks to Xcoder for the previous answer, i have fix the code so the image won't stop
func updateImage() { if arrowIndex >= 0 && arrowIndex < 4 { arrowImage.image = UIImage(named: arrowArray[arrowIndex]) arrowIndex += 1 if arrowIndex == 4 { arrowIndex = 0 } }else{ arrowIndex = 0 } } @IBAction func buttonClicked(_ sender: UIButton) { updateImage() }