MBProgressHud与gif图像

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

使用最新的MBProgressHUD和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图像: 装载机

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

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

链接到教程

希望它有所帮助……

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

我在Swift中创建了两个函数。 文件名是Globle.Swift。

 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)