Brad
Brad

Reputation: 57

Swift 2.0 - Using a gesture swipe to cycle through 5 images in an array

I'm new to Swift and programming in general and to stackoverflow.

I have an array of 5 images that I want to cycle through with a gesture swipe. Starting with the first one, ending on the fifth one and then having it just return to the first one again with the ability to just keep cycling through 1-5 over and over and over again.

Right now with help of google and tutorials I have gesture swipe working, and I have it displaying one of those images randomly with each swipe.

Here's an example of the lines making the image change randomly using gesture swipe:

//placed under class ViewController: UIViewController

@IBOutlet weak var picture: UIImageView!

var myArray:[String] = ["img1", "img2", "img3", "img4", "img5"]

//placed under override func didReceiveMemoryWarning() & super...()

@IBAction func pictureSwipe(sender: UISwipeGestureRecognizer) {
   let pictureRandom:Int = Int(arc4random_uniform(5))
   let pictureString:String = self.myArray[pictureRandom]
   self.picture.image = UIImage(named: pictureString)

}

Even if somebody could kind of point me in the right direction. This code works but I don't want random images. I am looking for it to cycle through "img1" thru to "img5" and start all over again if the user continues swiping. Any help or guidance is greatly appreciated.

Thank you

Upvotes: 0

Views: 1985

Answers (2)

aahrens
aahrens

Reputation: 5590

You'll want to keep a variable to control the current index into your string array. Then based on the direction of the swipe you'll want to decrement or increment it by 1. Try something like this.

private var myArray = ["img1", "img2", "img3", "img4", "img5"]
private var index = 0
@IBOutlet weak var picture: UIImageView!

@IBAction func pictureSwipe(sender: UISwipeGestureRecognizer) {
    let updateIndex = sender.direction == .Left ? 1 : -1
    index += updateIndex

    if index >= myArray.count {
        // Went past the array bounds. start over
        index = 0
    } else if index < 0 {
        // Jump to the back of the array
        index = myArray.count - 1
    }

    let pictureString = myArray[index]
    self.picture.image = UIImage(named: pictureString)
}

Upvotes: 1

Pop Flamingo
Pop Flamingo

Reputation: 2191

To cycle to your array, you can add a variable keeping track of your the array index of the current displayed image and increase it every time you do the gesture, then, when the index is equal to five, reset it to 0.

Here is an example :

//placed under class ViewController: UIViewController

var index = 0

@IBOutlet weak var picture: UIImageView!



var myArray:[String] = ["img1", "img2", "img3", "img4", "img5"]

//placed under override func didReceiveMemoryWarning() & super...()

@IBAction func pictureSwipe(sender: UISwipeGestureRecognizer) {


   let pictureString:String = self.myArray[index]
   self.picture.image = UIImage(named: pictureString)

   index = (index < myArray.count-1) ? index+1 : 0

}

As you can see I am here using a ternary operator, enabling me to do all of the incrementing and reset stuff in just one line.

Upvotes: 0

Related Questions