gpt4 book ai didi

swift - UIImageView 的延迟时间

转载 作者:行者123 更新时间:2023-11-30 13:13:18 25 4
gpt4 key购买 nike

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

覆盖 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])!)

}
}
}

谢谢

最佳答案

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])!)
}

}
}
}

关于swift - UIImageView 的延迟时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38499886/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com