延迟UIImageView

时间:2016-07-21 09:12:39

标签: swift swipe-gesture

当我将图像索引从collectionView发送到另一个viewController时,我有一个图像数组,这个viewController在全屏显示这个图像,我让用户能够在图像之间滑动但刷卡上的问题 更改图像之间的滑动非常快我需要在图像更改该问题的任何解决方案时延迟UIImageView上的时间吗?

以下代码:

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

覆盖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(手势: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])!)

        }
    }
}

谢谢

1 个答案:

答案 0 :(得分: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])!)
        }

      }
   }
}