AnimatedImageView 做 (先暂停的 gif 动画 )

邵君植
2023-12-01

KingFisher 的 AnimatedImageView,做 gif 动画,先暂停

代码部分:

    lazy var animatedPart: AnimatedImageView = {
        let img = AnimatedImageView()
        if let src = Bundle.main.url(forResource: "xx", withExtension: "gif"){
            img.kf.setImage(with: src)
        }
        return img
    }()
    
    
    var bePaused = true{
        didSet{
            if bePaused{
                animatedPart.stopAnimating()
            }
            else{
                animatedPart.startAnimating()
            }
        }
    }

关闭默认动画

处理 AnimatedImageView.swift 文件:

删掉一行 didMove()

// Reset the animator.
    private func reset() {
        animator = nil
        if let imageSource = image?.kf.imageSource {
            let targetSize = bounds.scaled(UIScreen.main.scale).size
            let animator = Animator(
                imageSource: imageSource,
                contentMode: contentMode,
                size: targetSize,
                framePreloadCount: framePreloadCount,
                repeatCount: repeatCount,
                preloadQueue: preloadQueue)
            animator.delegate = self
            animator.needsPrescaling = needsPrescaling
            animator.backgroundDecode = backgroundDecode
            animator.prepareFramesAsynchronously()
            self.animator = animator
        }
        // didMove()
    }

删掉

   override open func didMoveToWindow() {
        super.didMoveToWindow()
        didMove()
    }
    
    override open func didMoveToSuperview() {
        super.didMoveToSuperview()
        didMove()
    }

因为,走这一行

img.kf.setImage(with: src)

会走

reset()

接着走

didMove()

开启动画


添加视图 animatedPart

view.addSubview(animatedPart)

会走

override open func didMoveToSuperview() {
接着走

didMove()

开启动画

 类似资料: