不想用第三方的指示器,感觉有点大,自己写了一个简单的活动指示器,目前只有两种效果。效果如图

第一种:

第二种

第二种可以随着提示文字的增多而变长

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的更多相关文章

随机推荐

  1. Dennis与Ken爷爷的UNIX/C世界

     沉寂了很久了,时间在不断地逝去,转眼又到了新的一年,2013的发生了太多,Beta版本.辞职.职位转换.ARM.Driver.初级厨艺.Dx11.GPU.CPU.登山.GNU/Linux.Cross ...

  2. VMware虚拟机无法识别U盘解决方案

    1. 本机情况: Win7操作系统,VMware虚拟机,虚拟机版本:VMware 7.1,安装Ubuntu10.10,现要求在主机上插入U盘,在虚拟机中显示.   2. 遇到问题: U盘只在Win7主 ...

  3. Ubuntu中QT使用FFmpeg的奇怪问题

    FFmpeg都已经编译安装好了,QT的程序中调用av_register_all却总是在链接阶段报错,经过长时间的摸索,发现时静态链接库的问题,网上给出的答案都只能解决部分问题,所需的全部链接库如下: ...

  4. python导入自定义模块

    上网查了下资料和自己实验了下,有几个方法: 1.如果导入的模块和主程序在同个目录下,直接import就行了 2.如果导入的模块是在主程序所在目录的子目录下,可以在子目录中增加一个空白的__init__ ...

  5. 记一个dynamic的坑

    创建一个控制台程序和一个类库, 在控制台创建一个匿名对象,然后再在类库中访问它,代码如下: namespace ConsoleApplication1 { class Program { static ...

  6. sql order by 排序多个字段

    order by 多个字段,每个字段后面都有排序方式,默认ASC 例如:select table a order by a.time1 ,a.time2 desc,a.time3 asc

  7. Javascript起源...

    Javascript的设计思路是这样的: (1)借鉴C语言的基本语法: (2)借鉴Java语言的数据类型和内存管理: (3)借鉴Scheme语言,将函数提升到"第一等公民"(fir ...

  8. SSL握手步骤【收藏】

    http://www.codeweblog.com/ssl-handshake-process-of-interaction-and/ SSL to send a message in the fol ...

  9. IT168关于敏捷开发采访

    1.我们知道敏捷开发是一套流程和方法的持续改进,通过快速迭代的方式交付产品,从而控制和降低成本.但是在实行敏捷初期,往往看不到很好的效果.这里面,您觉得问题主要出在哪?团队应如何去解决问题?金根:我认 ...

  10. Android学习笔记(第二篇)View中的五大布局

    PS:人不要低估自己的实力,但是也不能高估自己的能力.凡事谦为本... 学习内容: 1.用户界面View中的五大布局... i.首先介绍一下view的概念   view是什么呢?我们已经知道一个Act ...