LGLProgressHUD
不想用第三方的指示器,感觉有点大,自己写了一个简单的活动指示器,目前只有两种效果。效果如图
第一种:
第二种
第二种可以随着提示文字的增多而变长
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的更多相关文章
随机推荐
- Android-NDK编译:cocos2d-x(三) eclipse 导入工程
NDK 编译后,用eclipse导入cocos2d-x工程 菜单[File]-->[New]-->[Project] ,弹出New Project 对话框 窗口下方 选 [Android] ...
- Python基本语句
x = 1 y = 1 dataf = 1.0 def fun1(): print("fun1") class ClassB(): def __init__(self): prin ...
- 为Ubuntu笔记本电脑设置WiFi热点共享上网
该文由土木坛子转译而来,说是转译,其实看截图就可以方便的设置,没有任何命令,全是图形界面,方便容易.我们都知道怎样在 windows 7 系统上如何设计 Wifi 热点,当你只有一条网线,多台计算机的 ...
- 使用 AFNetworking 进行 XML 和 JSON 数据请求
(1)XML 数据请求 使用 AFNetworking 中的 AFHTTPRequestOperation 和 AFXMLParserResponseSerializer,另外结合第三方框架 XMLD ...
- EDM博主笔记:EDM邮件营销的几个细节问题
其实说起EDM邮件营销很多做过的人都知道,目前国内邮件营销的效果其实是比较差的,为什么?因为国内没有多少使用邮件的习惯,如果不是工作所需估计很多的人都几天不碰邮件了,但是反观国外 邮件是其日常的一部分 ...
- 全国DNS服务器IP地址【电信、网通、铁通】
免费DNS地址: 114DNS:114.114.114.114(推荐国内使用) Google DNS:8.8.8.8(国外) ************************************* ...
- #ifdef 和 #if defined 的区别 -- 转
#ifdef 和 #if defined 的区别在于,后者可以组成复杂的预编译条件,比如 #if defined (AAA) && defined (BBB) xxxxxxxxx #e ...
- Ubuntu桌面版本和服务器版本之间的区别(转载)
转载自:http://blog.csdn.net/fangaoxin/article/details/6335992 http://www.linuxidc.com/Linux/2010-11/297 ...
- php添加数据到xml文件的例子
php添加数据到xml文件中 时间:2015-12-17 06:30:37来源:网络 导读:php添加数据到xml文件中 xml文件:stu.xml: 复制代码代码如下: <?xml ver ...
- zepto - slice
var ss = ['1', '2', '3', '4', '5', '6']; console.log(ss.slice(2,4));