不想用第三方的指示器,感觉有点大,自己写了一个简单的活动指示器,目前只有两种效果。效果如图
第一种:
第二种
第二种可以随着提示文字的增多而变长
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 / - , HEIGHT / - , , )];
_alertView.backgroundColor = BACKGROUNDCOLOR;
_alertView.layer.masksToBounds = YES;
_alertView.layer.cornerRadius = ; _activity = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(, , , )];
[_activity setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleWhiteLarge];
[_alertView addSubview:_activity]; _alertText = [[UILabel alloc] initWithFrame:CGRectMake(, , , )];
_alertText.text = @"正在加载...";
_alertText.textAlignment = NSTextAlignmentCenter;
_alertText.textColor = [UIColor whiteColor];
_alertText.font = TEXTFONT();
[_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 = ;
CGSize textSize = [self sizeWithText:message font:TEXTFONT() maxW:];
_alertText = [[UILabel alloc] initWithFrame:CGRectMake(, , textSize.width, textSize.height)];
_alertText.font = TEXTFONT();
_alertText.textColor = [UIColor whiteColor];
_alertText.textAlignment = NSTextAlignmentCenter;
_alertText.text = message;
[_alertView addSubview:_alertText];
_alertView.frame = CGRectMake(MAX(WIDTH / - (CGRectGetMaxX(_alertText.frame) + ) / , WIDTH / - (WIDTH - ) / ), HEIGHT / - (textSize.height + ) / , MIN(CGRectGetMaxX(_alertText.frame) + , WIDTH - ),textSize.height + );
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
LGLProgressHUD的更多相关文章
随机推荐
-
jquery修改Switchery复选框的状态
script //选择框 var mySwitch; /* * 初始化Switchery * * classNmae class名 */ function initSwitchery(classNam ...
-
BZOJ1031: [JSOI2007]字符加密Cipher
传送门 后缀数组模板题 //BZOJ 1031 //by Cydiater //2016.9.21 #include <iostream> #include <cstring> ...
-
SpringMVC报错The request sent by the client was syntactically incorrect ()
springmvc数据绑定出的错 在数据绑定的时候一定要主意Controller方法中的参数名和jsp页面里的参数名字是否一致或者按照绑定的规范来写, 如果不一致,可能回报如下错误: The requ ...
-
[转]Linux文件权限详解
转自:http://blog.chinaunix.net/uid-25052030-id-174343.html 在linux中的每一个文件或目录都包含有访问权限,这些访问权限决定了谁能访问和如何访问 ...
-
C++中把string转成char
c_str函数的返回值是const char*的,不能直接赋值给char*, c++语言提供了两种字符串实现,其中较原始的一种只是字符串的c语言实现. 与C语言的其他部分一样,它在c+的所有实现中可用 ...
-
CodeForces 678D Iterated Linear Function
简单矩阵快速幂. #include<cstdio> #include<cstring> #include<cmath> #include<algorithm& ...
-
Supervisor 安装及配置管理uwsgi进程
Supervisor介绍 Supervisor 允许其用户在UNIX类操作系统上控制多个进程. 块如下: 方便 需要为每个进程实例编写rc.d脚本通常是不方便的. rc.d脚本是进程初始化/自动启动/ ...
-
POJ 1236 Network of Schools (Tarjan)
Network of Schools Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 22745 Accepted: 89 ...
-
mascara-1
来源:https://github.com/MetaMask/mascara (beta) Add MetaMask to your dapp even if the user doesn't hav ...
-
学习windows编程 day6 之模拟记事本
//短的函数最好定义为宏 #define BUFFER(x,y) *(y*cxBuffer+x+pBuffer)//取出一个字符 //字符消息 //WM_CHAR,WM_DEADCHAR,WM_SYS ...