带有 gif 图像的 MBProgressHud [英] MBProgressHud with gif image

查看:27
本文介绍了带有 gif 图像的 MBProgressHud的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用 gif 图像代替默认加载指示器吗?到目前为止,我正在使用此代码,但没有得到任何结果.任何人都可以建议这段代码有什么问题吗?

#import "UIImage+GIF.h"-(void) showLoadingHUD:(NSString *)title{[自我隐藏加载HUD];如果(!HUD){HUD = [MBProgressHUD showHUDAddedTo:self.window 动画: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 *旋转;旋转 = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];rotation.fromValue = [NSNumber numberWithFloat:0];rotation.toValue = [NSNumber numberWithFloat:(2 * M_PI)];旋转.持续时间 = 0.7f;//速度旋转.repeatCount = HUGE_VALF;//永远重复.可以是有限数.[HUD.customView.layer addAnimation:rotation forKey:@"Spin"];HUD.mode = MBProgressHUDModeCustomView;[HUD 显示:是];}

解决方案

使用

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

解决方案

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:

这篇关于带有 gif 图像的 MBProgressHud的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆