MBProgressHud与gif图像

时间:2017-02-21 05:34:29

标签: ios objective-c animated-gif mbprogresshud

我可以使用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];
}

4 个答案:

答案 0 :(得分:2)

使用MBProgressHUDSDWebImage的最新库来获取“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图像: loader

答案 1 :(得分:0)

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

这是一个关于创建动画图像的UIImageView的教程:为iOS应用程序创建自定义活动指示器

link to the tutorial

希望有所帮助...

答案 2 :(得分:0)

是的,您可以使用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;

答案 3 :(得分:0)

雨燕3 我在Swift中创建了两个函数。文件名是Globle.Swift。创建UIApplication.Swift文件并放置以下扩展名以获取topViewController

之后

公共扩展UIApplication {

类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)