title: Image Slideshow Swift ios
body: <p>I'm new to ios development. I am trying to make a simple fullscreen image slide show. On swipe left, the slideshow should show the next image, and swipe right the slideshow should show the previous image.</p>
  
  <p>I have it working, however, if I swipe in quick succession, I get a blank screen, almost as if the animations aren't keeping up, and then when I wait a moment and swipe again the image views speed up into place and works normally again. Any idea what I'm doing wrong? What is the best practice when it comes to implementing an image carousel like this with a dynamic amount of images (here they're hardcoded)? </p>
  
  <pre><code>import UIKit
  
  var imageArr = ["imageOne.jpg", "imageTwo.jpg", "imageThree.jpg", "imageFour.jpg", "imageFive.jpg"]
  var imageIndex = 0;
  
  class ViewController: UIViewController {
  
      var currImage = UIImageView()
      var rightImage = UIImageView()
      var leftImage = UIImageView()
  
      override func viewDidLoad() {
          super.viewDidLoad()
          // Do any additional setup after loading the view, typically from a nib.
  
          var bounds:CGRect = UIScreen.mainScreen().bounds
          var width:CGFloat = bounds.size.width
          var height:CGFloat = bounds.size.height
  
  
          currImage.frame = CGRect(x: 0.0, y: 0.0, width: width, height: height)
          currImage.image = UIImage(named: imageArr[imageIndex])
  
          rightImage.frame = CGRect(x: width, y: 0.0, width: width, height: height)
          rightImage.image = UIImage(named: imageArr[imageIndex + 1])
  
          leftImage.frame = CGRect(x: -width, y: 0.0, width: width, height: height)
          leftImage.image = UIImage(named: imageArr[imageArr.count - 1])
  
          self.view.addSubview(currImage)
          self.view.addSubview(rightImage)
          self.view.addSubview(leftImage)
  
          var swipeLeft = UISwipeGestureRecognizer(target: self, action: "handleSwipe:")
          swipeLeft.direction = UISwipeGestureRecognizerDirection.Left
          self.view.addGestureRecognizer(swipeLeft)
  
          var swipeRight = UISwipeGestureRecognizer(target: self, action: "handleSwipe:")
          swipeRight.direction = UISwipeGestureRecognizerDirection.Right
          self.view.addGestureRecognizer(swipeRight)
      }
  
      override func didReceiveMemoryWarning() {
          super.didReceiveMemoryWarning()
          // Dispose of any resources that can be recreated.
  
      }
  
      let transitionManager = TransitionManager()
  
  
      func handleSwipe(gesture: UIGestureRecognizer) {
  
          var bounds:CGRect = UIScreen.mainScreen().bounds
          var width:CGFloat = bounds.size.width
          var height:CGFloat = bounds.size.height
  
          if let swipeGesture = gesture as? UISwipeGestureRecognizer {
  
              if (swipeGesture.direction == UISwipeGestureRecognizerDirection.Left ) {
  
                  UIView.animateWithDuration(0.5, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 0.2, options: UIViewAnimationOptions.CurveEaseIn, animations: {
  
  
                      self.currImage.frame = CGRect(x: -width, y: 0.0, width: width, height: height)
                      self.rightImage.frame = CGRect(x: 0.0, y:0.0, width: width, height: height)
  
                      }, completion: { finished in
  
                          if (!finished) { return }
  
                          imageIndex++
                          imageIndex = imageIndex &lt;= imageArr.count-1 ? imageIndex : 0
  
                          var leftIndex = imageIndex - 1 &lt; 0 ? imageArr.count - 1 : imageIndex - 1
  
                          self.leftImage.image = UIImage(named: imageArr[leftIndex])
                          self.leftImage.frame = CGRect(x: -width, y: 0.0, width: width, height: height)
  
                          var tempImg = self.currImage
  
                          self.currImage = self.rightImage
  
                          self.rightImage = tempImg
  
                          self.rightImage.frame = CGRect(x: width, y: 0.0, width: width, height: height)
  
                          var rightIndex = imageIndex + 1 &gt; imageArr.count - 1 ? 0 : imageIndex + 1
                          self.rightImage.image = UIImage(named: imageArr[rightIndex])
  
                  })
              }
  
              if (swipeGesture.direction == UISwipeGestureRecognizerDirection.Right) {
  
  
                  UIView.animateWithDuration(0.5, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 0.2, options: UIViewAnimationOptions.CurveEaseIn, animations: {
  
                      self.currImage.frame = CGRect(x: width, y: 0.0, width: width, height: height)
                      self.leftImage.frame = CGRect(x: 0.0, y: 0.0, width: width, height: height)
  
                      }, completion: { finished in
  
  
                          imageIndex--
                          imageIndex = imageIndex &lt; 0 ? imageArr.count - 1 : imageIndex
  
                          var rightIndex = imageIndex + 1 &gt; imageArr.count - 1 ? 0 : imageIndex + 1
  
                          self.rightImage.image = UIImage(named: imageArr[rightIndex])
                          self.rightImage.frame = CGRect(x: width, y: 0.0, width: width, height: height)
  
                          var tempImg = self.currImage
  
                          self.currImage = self.tempImg
                          self.leftImage = tempCurr
  
                          self.leftImage.frame = CGRect(x: -width, y: 0.0, width: width, height: height)
                          var leftIndex = imageIndex - 1 &lt; 0 ? imageArr.count - 1 : imageIndex - 1
                          self.leftImage.image = UIImage(named: imageArr[leftIndex])
  
                  })
              }
  
          }
      }
  
  } 
  </code></pre>
  
  <p>Any help is much appreciated! </p>