Swipe back and forth through array of images Swift

14,789

Solution 1

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.


    }

}


}

Solution 2

You can make something like that.

First you need to set a position-counter which says, where you are at the moment. Important: You need to set the start to 0 if you want to start from the first element, because arrays start counting from 0:

var swipePosition = 0

Then, if you swipe forward, you need to check if the current swipePosition is bigger than the amount of images. To do that, you can use the count method of your array. But you have to subtract 1 because the position starts at 0. So if it is already at the highest or lowest position in your array you don't have to do anything. Otherwise you add or subtract one position:

//swipe forward
if swipePosition > imageList.count-1{
    //do nothing because it is already at the highest position
}else{
    swipePosition += 1
}

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

//swipe backward

if swipePosition == 0 {
    //Do nothing because it is already at start.
}else{
    swipePosition -= 1
}

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

Solution 3

`

@IBOutlet weak var img: UIImageView!    

var currentnImageIndex:NSInteger = 0

let arrayOfImages = ["Home Filled-50","Bullish-50","Line Chart Filled-50","Stack of Photos Filled-50","News-50","Download From Ftp Filled-50","Administrator Male Filled-50","Trophy Filled-50","Page Overview  Filled-50"]

override func viewDidLoad() {
    super.viewDidLoad()

    img.userInteractionEnabled = true//do not forget to right this line otherwise ...imageView's image will not move


    let swipeRight = UISwipeGestureRecognizer(target: self, action: "swipedRight:")
    swipeRight.direction = UISwipeGestureRecognizerDirection.Right
    img.addGestureRecognizer(swipeRight)

    let swipeLeft = UISwipeGestureRecognizer(target: self, action: "swipedRight:")
    swipeLeft.direction = UISwipeGestureRecognizerDirection.Left
    img.addGestureRecognizer(swipeLeft)

    img.image = UIImage(named: arrayOfImages[currentnImageIndex])

    // Do any additional setup after loading the view.
}

func swipedRight(gesture : UIGestureRecognizer){

    if let swipeGesture = gesture as? UISwipeGestureRecognizer{
        switch swipeGesture.direction{
        case UISwipeGestureRecognizerDirection.Right:
            print("User swiped right")

            if currentnImageIndex < arrayOfImages.count - 1 {
            ++currentnImageIndex
            }

            if currentnImageIndex < arrayOfImages.count  {
            img.image = UIImage(named: arrayOfImages[currentnImageIndex])
            }
        case UISwipeGestureRecognizerDirection.Left:
            print("User swiped left")

           // --currentnImageIndex

            if currentnImageIndex > 0{
                --currentnImageIndex
            }
            if currentnImageIndex >= 0{
                img.image = UIImage(named: arrayOfImages[currentnImageIndex])
            }
           // if curretnImageIndex

        default:
            break
        }
     `

Solution 4

@IBOutlet weak var imageView: UIImageView!
    var imageIndex:NSInteger = 0
    var maximages = 3
    var imageList: [String] = ["burger", "burger2", "burger3", "burger4"]

    override func viewDidLoad() {
        super.viewDidLoad()

        let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(swiped)) // put : at the end of method name
        swipeRight.direction = UISwipeGestureRecognizerDirection.right
        self.view.addGestureRecognizer(swipeRight)

        let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(swiped)) // put : at the end of method name
        swipeLeft.direction = UISwipeGestureRecognizerDirection.left
        self.view.addGestureRecognizer(swipeLeft)

        imageView.image = UIImage(named:"burger")
    }

    func swiped(gesture: UIGestureRecognizer) {

        if let swipeGesture = gesture as? UISwipeGestureRecognizer {

            switch swipeGesture.direction {

                case UISwipeGestureRecognizerDirection.right :
                    print("User swiped right")

                    // decrease index first

                    imageIndex -= 1

                    // check if index is in range

                    if imageIndex < 0 {

                        imageIndex = maximages
                    }

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

                case UISwipeGestureRecognizerDirection.left:
                    print("User swiped Left")

                    // increase index first

                    imageIndex += 1

                    // check if index is in range

                    if imageIndex > maximages {

                        imageIndex = 0
                    }

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

                default:
                    break //stops the code/codes nothing.
                }
            }
        }
}
Share:
14,789
Lukesivi
Author by

Lukesivi

Cofounder at www.getpenta.com, a bank account for businesses.

Updated on June 08, 2022

Comments

  • Lukesivi
    Lukesivi almost 2 years

    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!