swift MBProgressHUD加载gif或者apng的动图

效果图

给MBProgressHUD添加一个分类(extension)

extension MBProgressHUD {
    
    ///  MBProgressHUD 显示加载gif hud方法
    ///
    /// - parameter view:               hud where to show
    /// - parameter userInterface:      hud userInerface enable  default = true
    /// - parameter animated:           hud show with animation  default = true
    ///
    /// - returns: nil
    class func showGifAdded(to view:UIView!,userInterface:Bool = true,animated:Bool = true){
        //如果是gif可以使用sdwebImage的方法加载本地gif
        let image = UIImage.sd_animatedGIFNamed("loading")
        //如果是apng图片的话,使用YYKit框架中的YYImage加载apng动图
//        let image = YYImage(named: "loading")
//        let giftImgView =  YYAnimatedImageView(frame: CGRect(x: 0, y: 0,  80, height: 80))
//        giftImgView.image = image
        let giftImgView = UIImageView(image: image)
        let hud = MBProgressHUD.showAdded(to: view, animated: animated)
        hud?.color = .clear
        hud?.mode = .customView
        //userInteraction用来控制hud加载过程中用户是否可以做拖动点击操作,默认true
        hud?.isUserInteractionEnabled = userInterface
        hud?.customView = giftImgView
    }
}

使用方法:

MBProgressHUD.showGifAdded(to: view, animated: true)   //默认userInterface=true
MBProgressHUD.showGifAdded(to: view, userInterface: false, animated: true) //可以设置userInterface=false
原文地址:https://www.cnblogs.com/qqcc1388/p/6638088.html