问题
Can I use gif image instead of default loading indicator? I am using this code so far but not getting any result. Can anyone suggest what is wrong in 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];
}
回答1:
use latest libraries of MBProgressHUD and SDWebImage for "UIImage+GIF.h" and it is working fine
-(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:
回答2:
You can do this by creating a UIImageView that animates a set of images, and then set the customView property of your MBProgressHUD to be that UIImageView.
Here's a tutorial about creating a UIImageView that animates the images: Create Custom Activity Indicator for your iOS App
link to the tutorial
Hope it help...
回答3:
Yes, you can use gif images instead of default loading...
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:
Swift 3 I have created two functions in Swift. File name is Globle.Swift. After Create a UIApplication.Swift file and put the following extension for getting topViewController
public extension 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)
来源:https://stackoverflow.com/questions/42359456/mbprogresshud-with-gif-image