MBProgressHud with gif image

前端 未结 4 1881
栀梦
栀梦 2021-02-11 01:46

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?



        
4条回答
  •  无人共我
    2021-02-11 02:21

    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:

提交回复
热议问题