0

i have array of image when i send the index of image from collectionView to another viewController which is display this image in full screen and i give the user ability to swipe between images but the issue on the swipe the swipe between images when change it is very fast i need to delay time on UIImageView when the image is changed any solution on that issue ?

the code below:

var ImageIndex:Int = 0 // this is index image which i send it from previous view controller
var arrayOfUrlImageLarge:[String] = []// this array which contain all the url of images

override func viewDidLoad() {

    super.viewDidLoad()

imageView = UIImageView(image: UIImage(contentsOfFile: arrayOfUrlImageLarge[ImageIndex]))

    imageView.contentMode = UIViewContentMode.ScaleAspectFill

     let swipeGestureRight = UISwipeGestureRecognizer(target: self, action: #selector(ShowImageViewController.swipe(_:)))
    swipeGestureRight.direction = .Right
    let swipeGestureLeft = UISwipeGestureRecognizer(target: self, action: #selector(ShowImageViewController.swipe(_:)))
    swipeGestureLeft.direction = .Left
    self.imageView.addGestureRecognizer(swipeGestureLeft)
    self.imageView.addGestureRecognizer(swipeGestureRight)
 }

func swipe(gesture:UISwipeGestureRecognizer){

    if gesture.direction == .Right {

        if ImageIndex == 0 {
            imageView.image = UIImage(data: NSData(contentsOfFile: arrayOfUrlImageLarge[ImageIndex])!)
        }else {
            ImageIndex = ImageIndex - 1
            imageView.image = UIImage(data: NSData(contentsOfFile: arrayOfUrlImageLarge[ImageIndex])!)
        }
    }
    if gesture.direction == .Left{

        if ImageIndex >= arrayOfUrlImageLarge.count {
            ImageIndex = arrayOfUrlImageLarge.count - 1
            imageView.image = UIImage(data: NSData(contentsOfFile: arrayOfUrlImageLarge[ImageIndex])!)
        }else {
            ImageIndex = ImageIndex + 1
            if ImageIndex >= arrayOfUrlImageLarge.count {
                return
            }
            imageView.image = UIImage(data: NSData(contentsOfFile: arrayOfUrlImageLarge[ImageIndex])!)

        }
    }
}

Thank you

1 Answer 1

0
func swipe(gesture:UISwipeGestureRecognizer){

   let delay = 4.5 * Double(NSEC_PER_SEC)
   let time = dispatch_time(DISPATCH_TIME_NOW, Int64(delay))
   dispatch_after(time, dispatch_get_main_queue()) {

       if gesture.direction == .Right {

        if ImageIndex == 0 {
           imageView.image = UIImage(data: NSData(contentsOfFile: arrayOfUrlImageLarge[ImageIndex])!)
        }
        else {
          ImageIndex = ImageIndex - 1
          imageView.image = UIImage(data: NSData(contentsOfFile: arrayOfUrlImageLarge[ImageIndex])!)
        }
       }
      if gesture.direction == .Left{

        if ImageIndex >= arrayOfUrlImageLarge.count {
           ImageIndex = arrayOfUrlImageLarge.count - 1
           imageView.image = UIImage(data: NSData(contentsOfFile: arrayOfUrlImageLarge[ImageIndex])!)
       }
       else {
         ImageIndex = ImageIndex + 1
         if ImageIndex >= arrayOfUrlImageLarge.count {
            return
         }
        imageView.image = UIImage(data: NSData(contentsOfFile: arrayOfUrlImageLarge[ImageIndex])!)
        }

      }
   }
}
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you very much please where i put this method in viewdidload?
try in the same method
check the updated answer it will execute after the duration
in the comment animation what am doing ? fade?
you dont need to animate anything.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.