首页 文章

延迟UIImageView的时间

提问于
浏览
0

当我将图像索引从collectionView发送到另一个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

override func viewDidLoad(){

super.viewDidLoad()

imageView = UIImageView(图片: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
    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])!)
            }
    
          }
       }
    }
    

相关问题