美文网首页
iOS UICollectionView 显示帧动画

iOS UICollectionView 显示帧动画

作者: 怀可 | 来源:发表于2021-04-25 09:49 被阅读0次

利用 UIImageView 的 animationImages 显示帧动画,每个 cell 上都显示,经过测试,会出现如下几个问题:

  1. 滑动 collectionView 后部分 cell 上的 imageView 不再播放动画
  2. 点按 cell 跳转到另一个界面,再返回,这个 cell 上的 imageView 不再播放动画

解决:
在自定义 UICollectionViewCell 中加代码:

    override func prepareForReuse() {
        super.prepareForReuse()
        endPlayImages()
    }
    
    var images: [UIImage]? // cell 缓存 images
    func playImages(_ images: [UIImage]? = nil) {
        if images != nil {
            self.images = images
        }
        
        guard let imgs = self.images else {
            return
        }
        previewIV.animationImages = imgs
        previewIV.animationDuration = TimeInterval(imgs.count * 1)
        previewIV.highlightedAnimationImages = imgs
        previewIV.startAnimating()
    }
    
    func endPlayImages() {
        previewIV.stopAnimating()
    }

然后到 UIViewController 中添加代码:

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        playAllImages() // 点到别的界面,返回,需要重新播放动画
    }
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        playAllImages() // 滑动后重新播放动画
    }

 extension ViewController: UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        playImage(collectionView: collectionView, indexPath: indexPath)
    }

    func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
        playImage(collectionView: collectionView, indexPath: indexPath)
    }

    func collectionView(_ collectionView: UICollectionView, didHighlightItemAt indexPath: IndexPath) {
        playImage(collectionView: collectionView, indexPath: indexPath)

    }

    func collectionView(_ collectionView: UICollectionView, didUnhighlightItemAt indexPath: IndexPath) {
        playImage(collectionView: collectionView, indexPath: indexPath)
    }

}


extension ViewController {
    func playAllImages() {
        let visibleCellIndex = self.galleryTypesView.locolCollection.indexPathsForVisibleItems
        
        for (i,cell) in self.galleryTypesView.locolCollection.visibleCells.enumerated()  {
            let indexPath = visibleCellIndex[i]
            if localWorks[indexPath.row].type == .animation {
                let cardCell = cell as! CardCollectionCell
                cardCell.playImages()
            }
        }
    }
    
    func playImage(collectionView: UICollectionView, indexPath: IndexPath) {
        let cardCell = collectionView.dequeueReusableCell(withReuseIdentifier: CardCollectionCell.description(), for: indexPath) as! CardCollectionCell
        cardCell.playImages()
    }
}

相关文章

  • iOS UICollectionView 显示帧动画

    利用 UIImageView 的 animationImages 显示帧动画,每个 cell 上都显示,经过测试...

  • iOS Animation创建及使用

    iOS 实现的基本动画 头尾式动画 2.block动画的方法 iOS显示关键帧动画 关键帧动画 动画的创建和使用 ...

  • iOS-动画知识梳理

    学习及实践笔记 记录iOS动画的学习及实践 目录 显示层(UIView)动画初级动画关键帧动画逐帧动画Gif动画的...

  • iOS动画不响应点击的问题

    iOS动画分为显性动画和隐性动画两种。 显示动画动画分为几类:基础动画、关键帧动画、动画组、转场动画。各个类的关系...

  • UIKit动画

    ios动画 1)序列帧动画是由多个动画帧按照一定的间隔时间逐帧连续播放而形成的动画ViewController.m...

  • iOS 关键帧动画

    级别:★★☆☆☆标签:「iOS CAKeyframeAnimation」「iOS 关键帧动画」「CAKeyfram...

  • iOS 10 UICollectionView cell错位

    问题:在iOS 10的UICollectionView的超出屏幕的Cell会显示错位,而在iOS 11上显示正常。...

  • [ WWDC2018 ] - 高性能 AutoLayout Hi

    UICollectionView性能对比,item自动适配大小,iOS 11看上去有掉帧卡顿的现象,iOS 12表...

  • 1. 控制动画

    《iOS编程》第27章 控制动画 学习笔记 基础动画 关键帧动画

  • 《iOS动画》读书笔记·内容层动画

    《iOS动画》读书笔记·前序《iOS动画》读书笔记·显示层动画《iOS动画》读书笔记·内容层动画《iOS动画》读书...

网友评论

      本文标题:iOS UICollectionView 显示帧动画

      本文链接:https://www.haomeiwen.com/subject/smmsrltx.html