2017-02-21 43 views
2

我可以使用gif圖像而不是默認加載嗎?我到目前爲止使用此代碼,但沒有得到任何結果。任何人都可以提出這個代碼有什麼問題嗎?MBProgreeHud與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]; 
} 
+0

檢查:https://www.cocoacontrols.com/controls/gifprogresshud –

+0

@AntonyRaphel檢查了這一點。我想知道我可以使用MBProgressHUD來實現這個功能嗎? – Niharika

+0

代碼添加爲ans –

回答

1

使用最新的圖書館MBProgressHUDSDWebImage「的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做到這一點,然後設置你的MBProgressHUD的customView屬性是UIImageView的。

這裏有一個教程有關創建動畫的圖像一個UIImageView:你的iOS應用

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;