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

第一种:

第二种

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

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. Spring3系列13-Controller和@RequestMapping

    Spring3系列13-Controller和@RequestMapping Controller返回值,String或者ModelAndView @RequestMapping关联url @Requ ...

  2. 上海华魏光纤传感科技有限公司 招聘 《.NET研发工程师》

    代友招聘 <.NET研发工程师> **** 公司简介 **** 上海华魏光纤传感技术有限公司成立于2001年,注册资金1458.16万人民币,专业从事光纤传感技术的研究开发,为交通.市政等 ...

  3. 构建基于WinRT的WP8.1 App 01:页面导航及页面缓存模式

    本篇博文主要阐述基于Windows Runtime的Windows Phone 应用页面间导航相关知识,主要分为以下几个方面: Window.Frame和Page概览 页面间实现跳转 处理物理后退键 ...

  4. CSS 会被继承的属性

    文本 color(颜色,a元素除外) direction(方向) font(字体) font-family(字体系列) font-size(字体大小) font-style(用于设置斜体) font- ...

  5. 【学】SoapExtension 学习

    http://msdn.microsoft.com/zh-cn/library/System.Web.Services.Protocols.SoapExtension_methods(v=vs.80) ...

  6. 使用SharePoint Designer定制开发员工工作日志系统实例!

    昨天已介绍了一篇<使用SharePoint Designer定制开发专家库系统实例!>,今天继续来介绍使用SharePoint Designer定制开发员工工作日志系统实例,主要功能包括填 ...

  7. 判断JavaScript对象为null或者属性为空

    http://blog.csdn.net/yiluoak_47/article/details/7766760 首先说下null与undefined区别: 对已声明但未初始化的和未声明的变量执行typ ...

  8. 二十九、EFW框架开发的系统支持SaaS模式和实现思路

    回<[开源]EFW框架系列文章索引>        EFW框架源代码下载V1.3:http://pan.baidu.com/s/1c0dADO0 EFW框架实例源代码下载:http://p ...

  9. C++:不同类型的指针的本质与差异

    转自:http://blog.csdn.net/richerg85/article/details/10076365 指针的类型(The Type of a Pointer)            一 ...

  10. Android样式的开发:drawable汇总篇

    Android有很多种drawable类型,除了前几篇详细讲解的shape.selector.layer-list,还有上一篇提到的color.bitmap.clip.scale.inset.tran ...