我想使用alamofire从服务器上下载10张图像,并在uicollectionview的imageview..invalid loop中对该图像进行动画处理,就像在窗口10上的照片应用程序一样。有人知道吗?我已经用Google搜索出来了,但是找不到合适的答案..有人说使用计时器。.但是我认为这是不可行的方法。
if let employeeImage = employeeModelForDisplay?.employeeImageArray {
self.animateImage(at: 0 , imageArray: employeeImage)
}
func animateImage(at index: Int, imageArray:[String]) {
if index < imageArray.count {
self.badgeImageView.alpha = 0.3
self.badgeImageView.setImage(link: imageArray[index], imageType: .UserImage)
UIView.animate(withDuration: 3.0, delay: 0.5, options: [.transitionCrossDissolve], animations: {
self.badgeImageView.alpha = 1.0
}) { (completed) in
self.animateImage(at: index + 1, imageArray: imageArray)
}
} else {
//self.animateImage(at: 0, imageArray: imageArray)
}
}
`
我已经在uicollectionviewcell中对以上代码进行了编码。
答案 0 :(得分:0)
方法-1:
在animationImages
上设置imageView
,即
let imagesArray = [UIImage(named: "image"), UIImage(named: "image1"), UIImage(named: "image2")]
imageView.animationImages = imagesArray.compactMap({$0})
imageView.animationDuration = 2.0
imageView.startAnimating()
方法2:
您可以创建自己的动画,例如
let imagesArray = [UIImage(named: "image"), UIImage(named: "image1"), UIImage(named: "image2")]
管理图像动画的方法,
func animateImage(at index: Int) {
if index < imagesArray.count {
self.imageView.image = self.imagesArray[index]
self.imageView.alpha = 0
UIView.animate(withDuration: 2.0, delay: 0.0, options: [.curveEaseInOut], animations: {
self.imageView.alpha = 1.0
}) { (completed) in
self.animateImage(at: index + 1)
}
} else {
self.animateImage(at: 0)
}
}
使用index 0
(即
self.animateImage(at: 0)
编辑1:
要从images
中获得server
,然后将其设置为imageView
的动画,
func downloadImages() {
let imageUrls = [String]()
var imagesArray = [UIImage]()
let dispatchGroup = DispatchGroup()
dispatchGroup.notify(queue: DispatchQueue.main) {[weak self] in
self?.imageView.animationImages = imagesArray.compactMap({$0})
self?.imageView.animationDuration = 2.0
self?.imageView.startAnimating()
}
imageUrls.forEach { (imageUrl) in
if let url = URL(string: imageUrl) {
dispatchGroup.enter()
URLSession.shared.dataTask(with: url, completionHandler: { (data, urlResponse, error) in
if let data = data, let image = UIImage(data: data) {
imagesArray.append(image)
dispatchGroup.leave()
}
}).resume()
}
}
}
答案 1 :(得分:0)
下载图像后尝试
UIView.transition(with: cell.ImageView, duration: 0.5, options: .transitionCrossDissolve, animations:
{
if cell.ImageView.tag == indexPath.row
{
cell.ImageView.image = downloadedImage
}
else
{
}
})