Ios 在集合视图单元格中设置具有淡入淡出效果的图像动画
注意:我已经尝试了stack overflow中的所有匹配问题,但没有一个有助于解决此问题/场景,因此发布 我有一个集合视图,其中有许多单元格,每个单元格有一个图像视图。每个细胞将有不同的图像阵列,我需要通过淡入淡出效果一个接一个地显示。然而,当我第一次正确显示时,问题就出现了。第一个单元格的图像显示在任何其他单元格中。图像洗牌Ios 在集合视图单元格中设置具有淡入淡出效果的图像动画,ios,swift,image,animation,uicollectionview,Ios,Swift,Image,Animation,Uicollectionview,注意:我已经尝试了stack overflow中的所有匹配问题,但没有一个有助于解决此问题/场景,因此发布 我有一个集合视图,其中有许多单元格,每个单元格有一个图像视图。每个细胞将有不同的图像阵列,我需要通过淡入淡出效果一个接一个地显示。然而,当我第一次正确显示时,问题就出现了。第一个单元格的图像显示在任何其他单元格中。图像洗牌 extension ViewController: UICollectionViewDelegate,UICollectionViewDataSource,UICol
extension ViewController: UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return imagesArray.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectionCell", for: indexPath)
// let image = cell.viewWithTag(1) as! UIImageView
// let imgArr = imagesArray[indexPath.row]
// image.downloadImageAndAnimateIt(imageStringArray: imgArr)
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let size = self.collectionView.bounds.size.width / 2 - 5
return CGSize(width: size, height: size - 5)
}
func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
let image = cell.viewWithTag(1) as! UIImageView
let imgArr = imagesArray[indexPath.row]
image.downloadImageAndAnimateIt(imageStringArray: imgArr)
}
}
//=========== END of view controller
extension UIImageView {
func downloadImageAndAnimateIt(imageStringArray:[String]){
var imagesArray = [UIImage]()
for imgStr in imageStringArray{
imagesArray.append(UIImage(named: imgStr)!)
}
let dispatchGroup = DispatchGroup()
// dispatchGroup.notify(queue: DispatchQueue.main) {[weak self] in
// self?.animationImages = imagesArray.compactMap({$0})
// self?.animationDuration = 5.0
// self?.startAnimating()
// }
var photoCount = 0
var timer = Timer()
dispatchGroup.notify(queue: DispatchQueue.main) {[weak self] in
timer = Timer.scheduledTimer(withTimeInterval: 2.0, repeats: true, block: { (timer) in
if (photoCount < imagesArray.count - 1){
photoCount = photoCount + 1;
}else{
photoCount = 0;
}
if let this = self {
UIView.transition(with: this, duration: 2.0, options: .transitionCrossDissolve, animations: {
this.image = imagesArray[photoCount]
}, completion: nil)
}
})
}
}
}
扩展视图控制器:UICollectionViewDelegate、UICollectionViewDataSource、UICollectionViewDelegateFlowLayout{
func collectionView(collectionView:UICollectionView,numberOfItemsInSection:Int)->Int{
返回imagesArray.count
}
func collectionView(collectionView:UICollectionView,cellForItemAt indexPath:indexPath)->UICollectionViewCell{
let cell=collectionView.dequeueReusableCell(withReuseIdentifier:“collectionCell”,for:indexath)
//将image=cell.viewWithTag(1)设为!UIImageView
//设imgArr=imagesArray[indexPath.row]
//image.downloadImageAndAnimateIt(imageStringArray:imgArr)
返回单元
}
func collectionView(collectionView:UICollectionView,布局collectionViewLayout:UICollectionViewLayout,sizeForItemAt indexPath:indexPath)->CGSize{
让size=self.collectionView.bounds.size.width/2-5
返回CGSize(宽度:大小,高度:大小-5)
}
func collectionView(collectionView:UICollectionView,willDisplay单元格:UICollectionViewCell,forItemAt indexPath:indexPath){
将image=cell.viewWithTag(1)设为!UIImageView
设imgArr=imagesArray[indexPath.row]
image.downloadImageAndAnimateIt(imageStringArray:imgArr)
}
}
//=============视图结束控制器
扩展UIImageView{
func下载ImageAndAnimateIT(imageStringArray:[字符串]){
var imagesArray=[UIImage]()
用于imageStringArray中的imgStr{
append(UIImage(名称:imgStr)!)
}
让dispatchGroup=dispatchGroup()
//dispatchGroup.notify(队列:DispatchQueue.main){[weak self]在
//self?.animationImages=imagesArray.compactMap({$0})
//自我?.animationDuration=5.0
//self?.startAnimating()
// }
var光电计数=0
var timer=timer()
dispatchGroup.notify(队列:DispatchQueue.main){[weak self]在
timer=timer.scheduledTimer(withTimeInterval:2.0,repeats:true,block:{(timer)in
if(光电计数
图像不应混洗,并且这些图像应与滚动前保持在同一单元格中。这里实际发生的是collectionView正在重用以前的单元格,因此,在下载图像时,以前单元格中的图像将加载到新单元格中。要删除此行为,您需要设置占位符图像或,您可以将图像设置为空
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectionCell", for: indexPath)
let image = cell.viewWithTag(1) as! UIImageView
image.image = "placeholderImage" // replace it with your place holder image
//Or
// image.image = UIImage()
let imgArr = imagesArray[indexPath.row]
image.downloadImageAndAnimateIt(imageStringArray: imgArr)
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let size = self.collectionView.bounds.size.width / 2 - 5
return CGSize(width: size, height: size - 5)
}
func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
}
下载图像并将其显示在
UITableView
/UICollectionView
单元格中时,您需要处理许多情况:
UIImageView
我强烈建议您为此使用一个库,例如SDWebImage您必须创建CustomCollectionCell类来分离代码,请尝试使用以下方法,对于下载图像,您也可以使用SDWebImage
extension ViewController: UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return imagesArray.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectionCell", for: indexPath) as! CollectionViewCell
cell.imgView.image = nil
cell.createImages(imageStringArray: imagesArray[indexPath.row])
cell.configureTimer()
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let size = self.collectionView.bounds.size.width / 2 - 5
return CGSize(width: size, height: (size * 1.5) - 5)
}
}
class CollectionViewCell: UICollectionViewCell {
override func awakeFromNib()
{
super.awakeFromNib()
// Initialization code
}
var imageArray:[UIImage] = []
var timer = Timer()
var photoCount: Int = 0
@IBOutlet weak var imgView: UIImageView!
func createImages(imageStringArray:[String])
{
self.imageArray.removeAll()
for imgStr in imageStringArray
{
self.imageArray.append(UIImage.init(named: imgStr) ?? UIImage.init())
}
}
func configureTimer()
{
self.timer.invalidate()
self.imgView.image = self.imageArray[self.photoCount] //Set Default Image First Time
self.timer = Timer.scheduledTimer(withTimeInterval: 2.0, repeats: true, block: { (timer) in
if (self.photoCount < self.imageArray.count - 1){
self.photoCount = self.photoCount + 1;
}else{
self.photoCount = 0;
}
if let imageView = self.imgView {
UIView.transition(with: imageView, duration: 2.0, options: .transitionCrossDissolve, animations: {
imageView.image = self.imageArray[self.photoCount]
}, completion: nil)
}
})
}
}
扩展视图控制器:UICollectionViewDelegate、UICollectionViewDataSource、UICollectionViewDelegateFlowLayout{
func collectionView(collectionView:UICollectionView,numberOfItemsInSection:Int)->Int{
返回imagesArray.count
}
func collectionView(collectionView:UICollectionView,cellForItemAt indexPath:indexPath)->UICollectionViewCell{
让cell=collectionView.dequeueReusableCell(带有reuseidentifier:“collectionCell”,for:indexath)作为!CollectionViewCell
cell.imgView.image=nil
createImages(imageStringArray:imagesArray[indexath.row])
cell.configureTimer()
返回单元
}
func collectionView(collectionView:UICollectionView,布局collectionViewLayout:UICollectionViewLayout,sizeForItemAt indexPath:indexPath)->CGSize{
让size=self.collectionView.bounds.size.width/2-5
返回CGSize(宽度:尺寸,高度:(尺寸*1.5)-5)
}
}
类CollectionViewCell:UICollectionViewCell{
重写func awakeFromNib()
{
super.awakeFromNib()
//初始化代码
}
var imageArray:[UIImage]=[]
var timer=timer()
var光电计数:Int=0
@IBVAR imgView:UIImageView!
func createImages(imageStringArray:[字符串])
{
self.imageArray.removeAll()
用于imageStringArray中的imgStr
{
self.imageArray.append(UIImage.init(名称:imgStr)??UIImage.init()
}
}
func configureTimer()
{
self.timer.invalidate()
self.imgView.image=self.imageArray[self.photoCount]//第一次设置默认图像
self.timer=timer.scheduledTimer(withTimeInterval:2.0,repeats:true,block:{(timer)in)
if(self.photoCount