2017-02-21 17 views
2

Могу ли я использовать gif-образ вместо загрузки по умолчанию? Я использую этот код до сих пор, но не получаю никакого результата. может ли кто-нибудь предположить, что не так в этом коде?MBProgreeHud with gif image

#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]; 
} 
+0

проверить это: https://www.cocoacontrols.com/controls/gifprogresshud –

+0

@ AnthonyRaphel проверил это. Я хочу знать, могу ли я реализовать это с помощью MBProgressHUD. – Niharika

+0

код добавлен как ans –

ответ

1

использовать последние библиотеки 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

0

Вы можете сделать это путем создания UIImageView, что оживляет набор изображений, а затем установить свойство customView вашего MBProgressHUD быть что UIImageView.

Вот учебник о создании UIImageView, что оживляет изображения: Создать пользовательский индикатор активности для IOS App

link to the tutorial

Надеется, что это поможет ...

0

Да, вы можете использовать GIF изображение вместо загрузки по умолчанию ...

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;