MBProgressHud with a gif image

Is it possible to use a gif image instead of a standard loading indicator? I am using this code so far, but I am not getting any result. Can anyone suggest what is wrong with this code?

#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]; } 
+3
source share
3 answers

use the latest MBProgressHUD and SDWebImage libraries for "UIImage + GIF.h" and it works great

 -(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]; } 

sample loader .gif image: loader

+2
source

You can do this by creating a UIImageView that animates a set of images, and then set the customView property of your MBProgressHUD as a UIImageView.

Here's a tutorial on creating a UIImageView that animates images: Create a custom activity indicator for your iOS app

textbook reference

Hope this helps ...

0
source

Yes, you can use gif images instead of loading by default ...

 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; 
0
source

Source: https://habr.com/ru/post/894364/


All Articles