MBProgressHud with gif image

前端 未结 4 768
傲寒
傲寒 2021-02-11 01:48

Can I use gif image instead of default loading indicator? I am using this code so far but not getting any result. Can anyone suggest what is wrong in this code?



        
4条回答
  •  粉色の甜心
    2021-02-11 02:36

    Swift 3 I have created two functions in Swift. File name is Globle.Swift. After Create a UIApplication.Swift file and put the following extension for getting topViewController

    public extension UIApplication {

    class func topViewController(_ viewController: UIViewController? = UIApplication.shared.keyWindow?.rootViewController) -> UIViewController? {

        if let nav = viewController as? UINavigationController {
            return topViewController(nav.visibleViewController)
        }
        if let tab = viewController as? UITabBarController {
            if let selected = tab.selectedViewController {
                return topViewController(selected)
            }
        }
        if let presented = viewController?.presentedViewController {
            return topViewController(presented)
        }
        if let slide = viewController as? SlideMenuController {
            return topViewController(slide.mainViewController)
        }
        return viewController
    }
    

    }

    open class func showLoadingSpinner(_ message: String? = "", sender: UIView? = UIApplication.topViewController()?.view) -> Void {
        let  HUD = MBProgressHUD.showAdded(to: sender!, animated: true)
        HUD.label.text = message
        HUD.bezelView.color = UIColor.clear
        let imageViewAnimatedGif = UIImageView()
        //The key here is to save the GIF file or URL download directly into a NSData instead of making it a UIImage. Bypassing UIImage will let the GIF file keep the animation.
        let filePath = Bundle.main.path(forResource: "loader", ofType: "gif")
        let gifData = NSData(contentsOfFile: filePath ?? "") as Data?
        imageViewAnimatedGif.image = UIImage.sd_animatedGIF(with: gifData)
        HUD.customView = UIImageView(image: imageViewAnimatedGif.image)
        var rotation: CABasicAnimation?
        rotation = CABasicAnimation(keyPath: "transform.rotation")
        rotation?.fromValue = nil
    // If you want to rotate Gif Image Uncomment 
      //  rotation?.toValue = CGFloat.pi * 2
        rotation?.duration = 0.7
        rotation?.isRemovedOnCompletion = false
        HUD.customView?.layer.add(rotation!, forKey: "Spin")
        HUD.mode = MBProgressHUDMode.customView
       // Change hud bezelview Color and blurr effect
        HUD.bezelView.color = UIColor.clear
        HUD.bezelView.tintColor = UIColor.clear
        HUD.bezelView.style = .solidColor
        HUD.bezelView.blurEffectStyle = .dark
        // Speed
        rotation?.repeatCount = .infinity
        HUD.show(animated: true)
    
    }
    
    open class func dismissLoadingSpinner(_ sender: UIView? = UIApplication.topViewController()?.view) -> Void {
        MBProgressHUD.hide(for: sender!, animated: true)
    }
    
    
    // Call Function from your viewController to Show 
    Global.showLoadingSpinner(sender: self.view)
    // Call Function from your view controller to Hide
    Global.dismissLoadingSpinner(self.view)
    

提交回复
热议问题