MBProgressHud与gif图像 [英] MBProgressHud with gif image

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

问题描述

我可以使用gif图片而不是默认加载指示器吗?我到目前为止使用此代码但没有得到任何结果。任何人都可以建议这段代码有什么问题?

  #importUIImage + 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 = [CABasicAnimation animationWithKeyPath:@transform.rotation];
rotation.fromValue = [NSNumber numberWithFloat:0];
rotation.toValue = [NSNumber numberWithFloat:(2 * M_PI)];
rotation.duration = 0.7f; //速度
rotation.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:

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

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