- // Update to indeterminate indicator
- [indicator removeFromSuperview];
- self.indicator = MB_AUTORELEASE([[UIActivityIndicatorView alloc]
- initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge]);
- self.indicator = MB_AUTORELEASE([[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 70, 40)]);
- self.indicator.contentMode = UIViewContentModeScaleAspectFit;
- // [(UIActivityIndicatorView *)indicator startAnimating];
- [self addSubview:indicator];
- for (int i=0; i<24; i++) {
- [_loadingArray addObject:[UIImage imageNamed:[NSString stringWithFormat:@"loading_%d",i + 1]]];
- }
- //设置动画数组
- [(UIImageView *)self.indicator setAnimationImages:_loadingArray];
- //设置动画播放次数
- [(UIImageView *)self.indicator setAnimationRepeatCount:MAXFLOAT];
- //设置动画播放时间
- [(UIImageView *)self.indicator setAnimationDuration:1.5];
- //开始动画
- [(UIImageView *)self.indicator startAnimating];
现在很多APP的HUD已经都是自定义动画了,MBProgressHUD没有开放的API可以让我们直接使用,需要到.m文件中去修改源码。
首先在.m文件中找到
- (void)updateIndicators
方法在
if (mode == MBProgressHUDModeIndeterminate) {}
中自定义动画,然后上代码:
- <span style="font-size:18px;">if (mode == MBProgressHUDModeIndeterminate) {
- if (!isActivityIndicator) {
- // Update to indeterminate indicator
- [indicator removeFromSuperview];
- self.indicator = MB_AUTORELEASE([[UIActivityIndicatorView alloc]
- initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge]);
- self.indicator = MB_AUTORELEASE([[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 70, 40)]);
- self.indicator.contentMode = UIViewContentModeScaleAspectFit;
- // [(UIActivityIndicatorView *)indicator startAnimating];
- [self addSubview:indicator];
- for (int i=0; i<24; i++) {
- [_loadingArray addObject:[UIImage imageNamed:[NSString stringWithFormat:@"loading_%d",i + 1]]];
- }
- //设置动画数组
- [(UIImageView *)self.indicator setAnimationImages:_loadingArray];
- //设置动画播放次数
- [(UIImageView *)self.indicator setAnimationRepeatCount:MAXFLOAT];
- //设置动画播放时间
- [(UIImageView *)self.indicator setAnimationDuration:1.5];
- //开始动画
- [(UIImageView *)self.indicator startAnimating];
- }
- #if __IPHONE_OS_VERSION_MIN_REQUIRED >= 50000
- if ([indicator isKindOfClass:[UIActivityIndicatorView class]]) {
- [(UIActivityIndicatorView *)indicator setColor:self.activityIndicatorColor];
- }
- #endif
- }</span>
回复hhhh23456789:我是把_loadingArray放在MB初始化方法里面声明的
- (id)initWithView:(UIView *)view { NSAssert(view, @"View must not be nil.");_loadingArray = [NSMutableArray array];return [self initWithFrame:view.bounds];}