UIImageView 的延迟时间
Posted
技术标签:
【中文标题】UIImageView 的延迟时间【英文标题】:delay time on UIImageView 【发布时间】: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])!)
谢谢
【问题讨论】:
【参考方案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 的什么地方? 用同样的方法试试 检查持续时间后它将执行的更新答案 在评论动画里在做什么?褪色? 你不需要任何动画。以上是关于UIImageView 的延迟时间的主要内容,如果未能解决你的问题,请参考以下文章
如何在 UITableView 中实现类似 UIImageView 的延迟加载
在 UITableView 中使用带有 [UIImage resizableImageWithCapInsets] 的 UIImageView 会导致延迟