Matt
Matt

Reputation: 13

Swipe UIImageView array loop

i am new here and I am also a beginner in Swift!

I am trying to create an UIImageView to swipe left and right, when i get to the last image the app crash, the same crash happen if i am in the first image and try to swipe to the right! What i wanna do is when my array of Images reaches the last image send me back to the First image in a loop, can anyone help me please? Thank you in advance!!! Here is my code;

func swiped(gesture: UIGestureRecognizer) { CATransaction.begin()

        CATransaction.setAnimationDuration(animationDuration)
        CATransaction.setCompletionBlock {
            let delay = dispatch_time(DISPATCH_TIME_NOW, Int64(self.switchingInterval * NSTimeInterval(NSEC_PER_SEC)))
            dispatch_after(delay, dispatch_get_main_queue()) {
            }


        }

        let transition = CATransition()
        transition.type = kCATransitionFade
        chracters.layer.addAnimation(transition, forKey: kCATransition)

        CATransaction.commit()


        if let swipeGesture = gesture as? UISwipeGestureRecognizer {

            switch swipeGesture.direction {

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

                // decrease index

                imageIndex -= 1


                // check if index is in range

                if swipePosition > imageList.count-1{

                // Do Nothing
                } else {

                    swipePosition += 1
                }
                chracters.image = UIImage(named: imageList[imageIndex])

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

                // increase index first

                imageIndex += 1

                // check if index is in range

                if swipePosition == 0 {

                    //Do Nothing
                } else {

                    swipePosition -= 1
                }

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



            default:
                break;
                //stops the code



            }

        }


    }

Upvotes: 0

Views: 494

Answers (1)

Arun Gupta
Arun Gupta

Reputation: 2636

First your condition seems to be amiguous, you can use swipePosition itself instead of imageIndex. Secondly your chracters.image should inside the if/else block. Below is the updated snippet. If you want to use imageIndex you can change it accordingly. Also added the code to switch from last image to first image and first image to last.

if swipePosition > imageList.count-1{
    swipePosition = 0
    chracters.image = UIImage(named:imageList[swipePosition]
 } else {
   chracters.image = UIImage(named:imageList[swipePosition]
    swipePosition += 1
 }

case UISwipeGestureRecognizerDirection.Left: 
print("User swiped Left") 
// check if index is in range 
if  swipePosition == 0 { 
    swipePosition = imageList.count-1
    chracters.image = UIImage(named:imageList[swipePosition]
 } else {
     chracters.image = UIImage(named:imageList[swipePosition]
     swipePosition -= 1 
}

Upvotes: 1

Related Questions