【问题标题】:delay time on UIImageViewUIImageView 的延迟时间
【发布时间】:2016-07-21 09:12:39
【问题描述】:

当我将图像索引从 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(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])!)

        }
    }
}

谢谢

【问题讨论】:

    标签: swift swipe-gesture


    【解决方案1】:
    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])!)
            }
    
          }
       }
    }
    

    【讨论】:

    • 非常感谢我把这个方法放在 viewdidload 的什么地方?
    • 用同样的方法试试
    • 检查持续时间后它将执行的更新答案
    • 在评论动画里在做什么?褪色?
    • 你不需要任何动画。
    猜你喜欢
    • 2012-02-08
    • 1970-01-01
    • 2015-02-20
    • 1970-01-01
    • 2023-04-05
    • 2012-08-16
    • 1970-01-01
    • 2010-12-22
    • 1970-01-01
    相关资源
    最近更新 更多