Swipe vorwärts und rückwärts durch die Reihe von Bildern Swift

Ich habe ein array von Bildern, die ich möchte in der Lage sein, eine Bewegung nach vorwärts (nach Links), um zum nächsten Bild oder zurück (rechts), um das Vorherige Bild. Wenn Sie die imageList-trifft -1/out-of-range, die app stürzt ab. Ich habe Probleme, herauszufinden, die Logik zu halten, wie es in Reichweite ist.

Hier ist mein code:

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

Die swipe-gesten sind in meinen viewDidLoad() Methode, nicht sicher, ob dies der richtige Ort ist... :

    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.


        }

    }


}

Vielen Dank im Voraus!

InformationsquelleAutor Lukesivi | 2015-02-24

Schreibe einen Kommentar