//
//  PQGIFImageView.swift
//  PQSpeed
//
//  Created by SanW on 2020/9/2.
//  Copyright © 2020 BytesFlow. All rights reserved.
//

import UIKit

public class PQGIFImageView: UIImageView {
   public var imagesDara: [String]? {
        didSet {
            generateImages()
            displayGIF(1, Int.max)
        }
    }

    public var images: [UIImage] = Array<UIImage>.init()
    public func generateImages() {
        if imagesDara != nil, (imagesDara?.count ?? 0) > 0 {
            for item in imagesDara! {
                let image = UIImage(named: item)!
                images.append(image)
            }
        }
    }

    public func displayGIF(_ duration: TimeInterval, _ repeatCount: Int) {
        if !isAnimating {
            layer.removeAllAnimations()
            if images.count <= 0 {
                return
            }
            animationImages = images
            animationDuration = duration
            animationRepeatCount = repeatCount
            startAnimating()
        }
    }
}
