Search code examples
iosarraysswiftuiswipegesturerecognizer

Swipe back and forth through array of images Swift


I have an array of images that I want to be able to swipe forward (left) to the next image, or back (right) to the previous image. When the imageList hits -1/out of range, the app crashes. I'm having trouble of figuring out the logic of how to keep it within range.

Here is my code:

var imageList:[String] = ["image1.jpg", "image2.jpg", "image3.jpg"]
let maxImages = 2
var imageIndex: NSInteger = 1

The swipe gestures are in my viewDidLoad() method, not sure if this is the right place... :

    override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    var swipeRight = UISwipeGestureRecognizer(target: self, action: "swiped:") // put : at the end of method name
    swipeRight.direction = UISwipeGestureRecognizerDirection.Right
    self.view.addGestureRecognizer(swipeRight)

    var swipeLeft = UISwipeGestureRecognizer(target: self, action: "swiped:") // put : at the end of method name
    swipeLeft.direction = UISwipeGestureRecognizerDirection.Left
    self.view.addGestureRecognizer(swipeLeft)

    image.image = UIImage(named:"image1.jpg")

}


func swiped(gesture: UIGestureRecognizer) {

    if let swipeGesture = gesture as? UISwipeGestureRecognizer {

        switch swipeGesture.direction {

        case UISwipeGestureRecognizerDirection.Right :
            println("User swiped right")

        /*No clue how to make it go back to the previous image and 
        when it hits the last image in the array, it goes back to 
        the first image.. */

        case UISwipeGestureRecognizerDirection.Left:
            println("User swiped Left")


            if imageIndex > maxImages {

                imageIndex = 0

            }

            image.image = UIImage(named: imageList[imageIndex])

            imageIndex++



        default:
            break //stops the code/codes nothing.


        }

    }


}

Thanks a lot in advance!


Solution

  • First of all your image index should be set to zero since array elements starts from zero but thats not a source of your problem

    var imageIndex: NSInteger = 0
    

    then your swiped function should be like following

    func swiped(gesture: UIGestureRecognizer) {
    
    if let swipeGesture = gesture as? UISwipeGestureRecognizer {
    
        switch swipeGesture.direction {
    
        case UISwipeGestureRecognizerDirection.Right :
            println("User swiped right")
    
            // decrease index first
    
            imageIndex--
    
            // check if index is in range
    
            if imageIndex < 0 {
    
                imageIndex = maxImages
    
            }
    
           image.image = UIImage(named: imageList[imageIndex])
    
        case UISwipeGestureRecognizerDirection.Left:
            println("User swiped Left")
    
            // increase index first
    
            imageIndex++
    
            // check if index is in range
    
            if imageIndex > maxImages {
    
                imageIndex = 0
    
            }
    
           image.image = UIImage(named: imageList[imageIndex])
    
    
    
    
        default:
            break //stops the code/codes nothing.
    
    
        }
    
    }
    
    
    }