带有 gif 图像的 MBProgressHud

MBProgressHud with gif image

我可以使用 gif 图片代替默认加载指示器吗?到目前为止,我正在使用这段代码,但没有得到任何结果。任何人都可以建议这段代码有什么问题吗?

#import "UIImage+GIF.h"
-(void) showLoadingHUD:(NSString *)title
{
    [self hideLoadingHUD];
    if(!HUD){
        HUD = [MBProgressHUD showHUDAddedTo:self.window animated:YES];
    }
    [HUD setColor:[UIColor clearColor]];

    UIImageView *imageViewAnimatedGif = [[UIImageView alloc]init];
    imageViewAnimatedGif.image= [UIImage sd_animatedGIFNamed:@"martini_glass"];

    HUD.customView = [[UIImageView alloc] initWithImage:imageViewAnimatedGif.image];
    CABasicAnimation *rotation;
    rotation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
    rotation.fromValue = [NSNumber numberWithFloat:0];
    rotation.toValue = [NSNumber numberWithFloat:(2 * M_PI)];
    rotation.duration = 0.7f; // Speed
    rotation.repeatCount = HUGE_VALF; // Repeat forever. Can be a finite number.
    [HUD.customView.layer addAnimation:rotation forKey:@"Spin"];
    HUD.mode = MBProgressHUDModeCustomView;
    [HUD show:YES];
}

您可以通过创建一个为一组图像设置动画的 UIImageView,然后将您的 MBProgressHUD 的 customView 属性 设置为该 UIImageView 来实现。

这是一个关于创建动画图像的 UIImageView 的教程:为您的 iOS 应用程序

创建自定义 Activity 指示器

link to the tutorial

希望对您有所帮助...

是的,您可以使用 gif 图片代替默认加载...

MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
hud.label.text = @"loading…";
hud.mode = MBProgressHUDModeCustomView;
UIImage *image = [[UIImage imageNamed:@"toast_loading"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
UIImageView *imgView = [[UIImageView alloc] initWithImage:image];
CABasicAnimation *anima = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
anima.toValue = http://www.cnblogs.com/Apologize/p/@(M_PI*2);
anima.duration = 1.0f;
anima.repeatCount = 10;
[imgView.layer addAnimation:anima forKey:nil];
hud.customView = imgView;

hud.bezelView.color = [UIColor colorWithWhite:0.0 alpha:1];
// text color
hud.contentColor = [UIColor whiteColor];
hud.animationType = MBProgressHUDAnimationFade;

MBProgressHUD and SDWebImage 的最新库用于 "UIImage+GIF.h",它工作正常

-(void) showLoadingHUD:(NSString *)title {

    [HUD hideAnimated:true];
    HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];

    HUD.label.text = title;
    HUD.bezelView.color = [UIColor clearColor];
    UIImageView *imageViewAnimatedGif = [[UIImageView alloc]init];

    //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.
    NSString *filePath = [[NSBundle mainBundle] pathForResource: @"loader" ofType: @"gif"];
    NSData *gifData = [NSData dataWithContentsOfFile: filePath];
    imageViewAnimatedGif.image = [UIImage sd_animatedGIFWithData:gifData];

    HUD.customView = [[UIImageView alloc] initWithImage:imageViewAnimatedGif.image];
    CABasicAnimation *rotation;
    rotation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
    rotation.fromValue = [NSNumber numberWithFloat:0];
    rotation.toValue = [NSNumber numberWithFloat:(2 * M_PI)];
    rotation.duration = 0.7f; // Speed
    rotation.repeatCount = HUGE_VALF; // Repeat forever. Can be a finite number.
    rotation.removedOnCompletion = false;
    [HUD.customView.layer addAnimation:rotation forKey:@"Spin"];
    HUD.mode = MBProgressHUDModeCustomView;
    HUD.contentColor = [UIColor redColor];
    [HUD showAnimated:YES];
}

示例加载器 .gif 图片:

Swift 3 我在 Swift 中创建了两个函数。文件名为 Globle.Swift。创建一个 UIApplication.Swift 文件并添加以下扩展名以获取 topViewController

public 扩展 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)