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, width: 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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM