ios - 带有 gif 图像的 MBProgressHud

标签 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];
}

最佳答案

使用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 图像: loader

关于ios - 带有 gif 图像的 MBProgressHud,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42359456/

相关文章:

ios - 为什么UIView动画没有触发CALayer的addAnimation :forKey:?

iphone - 另一个 VC 上的“setInt”不起作用

ios - FFmpeg iOS -> 输出文件无效

c++ - "Objective-C is a superset of C more strictly than C++"到底是什么意思?

ios - 如何使用 AVFoundation 在 Objective-C 中创建动态图

php - 使用 PHP/CodeIgniter 动态检测动画 GIF

ios - 如何将 CAGradientLayer 添加到 CAShapeLayer?

ios - 我怎样才能用带有 UILabel 的 NSAttributedString 进行描边和填充

ios - 如何在 Xcode 9.2 中获取新的 iOS SDK?

c - 如何在 GTK+ 中启动和停止 GIF 动画 GtkImage