【问题标题】:MBProgressHud with gif image带有 gif 图像的 MBProgressHud
【发布时间】:2018-09-26 18:29:28
【问题描述】:

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

【问题讨论】:

  • @AntonyRapel 检查了这个。我想知道我可以使用 MBProgressHUD 来实现它吗?
  • 作为ans添加的代码
  • @AntonyRapel 谢谢.. 它有效!!!!!
  • 不客气!!

标签: ios objective-c animated-gif mbprogresshud


【解决方案1】:

"UIImage+GIF.h" 使用最新的 MBProgressHUDSDWebImage 库,它工作正常

-(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 图像:

【讨论】:

  • 有没有办法通过代码将.gif的大小设置为100*100?
  • @Mamta 设置这个变量的大小imageViewAnimatedGif
【解决方案2】:

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

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

link to the tutorial

希望对您有所帮助...

【讨论】:

    【解决方案3】:

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

    【讨论】:

      【解决方案4】:

      斯威夫特 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)
      

      【讨论】:

      • 我收到错误,类型“UIApplication”没有成员“topViewController”。你能帮忙吗?
      • @Nij 抱歉,我没有添加 UIApplication Extension 持有的 topViewController 函数。只需添加它,我已经编辑了我的帖子,再次查看。
      • 好的,谢谢您的回复。会通过 AppDelegate.window 得到它
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-01-04
      • 1970-01-01
      • 2019-08-11
      • 1970-01-01
      • 2018-06-19
      • 2014-04-04
      • 2014-11-22
      相关资源
      最近更新 更多