不想用第三方的指示器,感觉有点大,自己写了一个简单的活动指示器,目前只有两种效果。效果如图
第一种:
第二种
第二种可以随着提示文字的增多而变长
LGLProgressHUD.h
// // LGLProgressHUD.h // LGLProgress // // Created by 李国良 on 2016/10/8. // Copyright © 2016年 李国良. All rights reserved. // #import <UIKit/UIKit.h> @interface LGLProgressHUD : NSObject + (instancetype)shareProgressHUD; - (void)showProgress; - (void)showProgressWithMessage:(NSString *)message; - (void)stopProgress; @end
LGLProgressHUD.m
// // LGLProgressHUD.m // LGLProgress // // Created by 李国良 on 2016/10/8. // Copyright © 2016年 李国良. All rights reserved. /* 菊花样式的修改 typedef NS_ENUM(NSInteger, UIActivityIndicatorViewStyle) { UIActivityIndicatorViewStyleWhiteLarge, UIActivityIndicatorViewStyleWhite, UIActivityIndicatorViewStyleGray, }; */ #import "LGLProgressHUD.h" #define WIDTH [UIScreen mainScreen].bounds.size.width #define HEIGHT [UIScreen mainScreen].bounds.size.height #define BACKGROUNDCOLOR [UIColor colorWithRed:0 green:0 blue:0 alpha:0.3] #define TEXTFONT(a) [UIFont systemFontOfSize:a] @interface LGLProgressHUD () { UIView * _alertView; UIActivityIndicatorView * _activity; UILabel * _alertText; } @end @implementation LGLProgressHUD static id _instace; + (instancetype)shareProgressHUD { static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{// 只创建一次 _instace = [[self alloc] init]; }); return _instace; } + (id)allocWithZone:(struct _NSZone *)zone { static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{// 只初始化一次 _instace = [super allocWithZone:zone]; }); return _instace; } - (id)copyWithZone:(NSZone *)zone { return _instace; } - (void)showProgress { if (!_alertView) { UIWindow *window = [UIApplication sharedApplication].keyWindow; _alertView = [[UIView alloc] initWithFrame:CGRectMake(WIDTH / 2 - 50, HEIGHT / 2 - 50, 110, 110)]; _alertView.backgroundColor = BACKGROUNDCOLOR; _alertView.layer.masksToBounds = YES; _alertView.layer.cornerRadius = 10; _activity = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0, 0, 110, 80)]; [_activity setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleWhiteLarge]; [_alertView addSubview:_activity]; _alertText = [[UILabel alloc] initWithFrame:CGRectMake(0, 80, 110, 20)]; _alertText.text = @"正在加载..."; _alertText.textAlignment = NSTextAlignmentCenter; _alertText.textColor = [UIColor whiteColor]; _alertText.font = TEXTFONT(14); [_alertView addSubview:_alertText]; [_activity startAnimating]; [window addSubview:_alertView]; } } - (void)showProgressWithMessage:(NSString *)message { if (!_alertView) { _alertView = [[UIView alloc] init]; _alertView.backgroundColor = BACKGROUNDCOLOR; _alertView.layer.masksToBounds = YES; _alertView.layer.cornerRadius = 8; CGSize textSize = [self sizeWithText:message font:TEXTFONT(14) maxW:0]; _alertText = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, textSize.width, textSize.height)]; _alertText.font = TEXTFONT(14); _alertText.textColor = [UIColor whiteColor]; _alertText.textAlignment = NSTextAlignmentCenter; _alertText.text = message; [_alertView addSubview:_alertText]; _alertView.frame = CGRectMake(MAX(WIDTH / 2 - (CGRectGetMaxX(_alertText.frame) + 10) / 2, WIDTH / 2 - (WIDTH - 20) / 2), HEIGHT / 2 - (textSize.height + 20) / 2, MIN(CGRectGetMaxX(_alertText.frame) + 10, WIDTH - 20),textSize.height + 20); UIWindow * window = [UIApplication sharedApplication].keyWindow; [window addSubview:_alertView]; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.7 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [self stopProgress]; }); } } - (void)stopProgress { [_activity stopAnimating]; [_alertView removeFromSuperview]; _alertView = nil; } //计算文字的大小 maxW限制最大宽度 maxW 传MAXFLOAT,没有限制最大的宽度 - (CGSize)sizeWithText:(NSString *)text font:(UIFont *)font maxW:(CGFloat)maxW { NSMutableDictionary *attrs = [NSMutableDictionary dictionary]; attrs[NSFontAttributeName] = font; CGSize maxSize = CGSizeMake(maxW, MAXFLOAT); return [text boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size; } @end