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的更多相关文章
随机推荐
- mac或linux下xampp的mysql配置
在mac 下安装好xampp后,需要在终端命令行操作时,比如输入:mysql -u root -p,未正确配置前不会出现想要的输入密码提示,而是会提示: command not found 原来当你输 ...
- iOS开发问题之Could not instantiate class named NSLayoutConstraint
这个问题在ios6.0之前的版本中运行会出现,因为使用Xcode 4.5之后的版本新建项目默认是选中AutoLayout的,但这个特性是在iOS6.0之后的版本中才支持的. 解决办法是选中.stroy ...
- 关于android中线性布局的layout_gravity属性
当 android:orientation="vertical" 时, 只有水平方向的设置才起作用,垂直方向的设置不起作用.即:left,right,center_horizon ...
- 入手Cubieboard2之制作最小Linux系统
前言 昨天终于入手了一块Cubieboard2板子,今年4月入职从事的就是与之相关的工作,因此趁现在有时间就好好熟悉一下. 一.主机环境 1.PC主机WIN 7旗舰版 1.虚拟机VM7.0 2.ubu ...
- Git回滚远程版本
摘要: 原创出处:www.bysocket.com 泥瓦匠BYSocket 希望转载,保留摘要,谢谢! “房子是租的 但生活不是” 1.故事的开始 远程master分支下代码被不小心提交了很多垃圾代码 ...
- [转]安卓开发startservice 和bindservice详解
原文 作者:aikongmeng 来源:安卓中文网 博主暗表:搜到此文,终于为我解惑,bindService并不会真正启动service,不会调用onStartCommand!还需要再bind之前st ...
- SSAS:菜鸟摸门
官方:SSAS 多维模型 Analysis Services 多维解决方案使用多维数据集结构来分析多个维度之间的业务数据. 多维模式是 Analysis Services 的默认服务器模式. 它包括针 ...
- win7下IE主页无法修改,IE设置无法保存解决方案
转自:http://www.myhack58.com/Article/48/65/2012/34411.htm 经测,有效! 现象如下: 1.开启后,首先总是指向http://go.microsoft ...
- Xcode 中关于"#"的小知识
在代码中使用Autolayout时,大家都会使用NSDictionaryOfVariableBindings这个宏,这个宏可以生成一个变量名到变量值映射的Dictionary.比如NSDictiona ...
- JavaScript面试库
1.将一段字符串转成驼峰命名法. var str = "web-kit-index"; function to(str){ var j = str.split("-&qu ...