iOS 封装加载、提示窗progressHUD

释放双眼,带上耳机,听听看~!

封装了一个加载动画、提示信息弹窗HWProgressHUD,使用简单,方便修改,下面先看一下效果图:

简单说一下思路:自定义视图继承UIView,创建单例,回主线程显示,显示时创建UIWindow,将自身添加上去,显示加载动画、提示文字,消失时将创建的window移除。

简述一下用法,在HWProgressHUD.h中可以看到下面几个方法:

  • + (void)show; //显示转圈加载,调用dismiss、showMessage等方法隐藏,常用于已有视图上提交加载数据
  • + (void)showWhilePushing; //空页面显示加载动画,常用于未创建视图时加载数据,创建控件
  • + (void)showWhilePushing:(BOOL)pushing;  //加载动画,传YES显示空页面显示加载动画,传NO显示转圈加载动画
  • + (void)showMessage:(NSString *)message;  //显示文字,默认两秒后消失
  • + (void)showMessage:(NSString *)message duration:(NSTimeInterval)duration;  //显示文字,设置显示时长
  • + (void)dismiss;  //隐藏
下面贴上代码:
#import <UIKit/UIKit.h>
@interface HWProgressHUD : UIView
//显示转圈加载
+ (void)show;
//空页面显示加载动画
+ (void)showWhilePushing;
//加载动画,传YES显示空页面显示加载动画,传NO显示转圈加载动画
+ (void)showWhilePushing:(BOOL)pushing;
//显示文字,默认两秒
+ (void)showMessage:(NSString *)message;
//显示文字,设置时长
+ (void)showMessage:(NSString *)message duration:(NSTimeInterval)duration;
//隐藏
+ (void)dismiss;
@end
/*** ---------------分割线--------------- ***/
#import "HWProgressHUD.h"
#define KPLabelMaxW 240.0f
#define KPLabelMaxH 300.0f
#define KDefaultDuration 2.0f
#define KMainW [UIScreen mainScreen].bounds.size.width
#define KMainH [UIScreen mainScreen].bounds.size.height
@interface HWProgressHUD ()
@property (nonatomic, strong) UIWindow *pWindow;
@property (nonatomic, weak) UILabel *pLabel;
@property (nonatomic, weak) UIImageView *pImageView;
@property (nonatomic, weak) UIView *backView;
@end
@implementation HWProgressHUD
+ (HWProgressHUD *)sharedView
{
static HWProgressHUD *sharedView;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedView = [[HWProgressHUD alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
});
return sharedView;
}
- (id)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
}
return self;
}
+ (void)show
{
[[HWProgressHUD sharedView] showWithMessage:nil duration:KDefaultDuration pushing:NO];
}
+ (void)showWhilePushing
{
[[HWProgressHUD sharedView] showWithMessage:nil duration:KDefaultDuration pushing:YES];
}
+ (void)showWhilePushing:(BOOL)pushing
{
[[HWProgressHUD sharedView] showWithMessage:nil duration:KDefaultDuration pushing:pushing];
}
+ (void)showMessage:(NSString *)message
{
[[HWProgressHUD sharedView] showWithMessage:message duration:KDefaultDuration pushing:nil];
}
+ (void)showMessage:(NSString *)message duration:(NSTimeInterval)duration
{
[[HWProgressHUD sharedView] showWithMessage:message duration:duration pushing:nil];
}
+ (void)dismiss
{
[[HWProgressHUD sharedView] dismiss];
}
- (void)showWithMessage:(NSString *)message duration:(NSTimeInterval)duration pushing:(BOOL)pushing
{
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.01f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
if (!self.superview) [self.pWindow addSubview:self];
[self.pWindow makeKeyAndVisible];
if (message) {
if (_pImageView) {
_pImageView.hidden = YES;
[self stopLoadingAnimation];
}
self.pLabel.text = message;
CGSize stringSize = [message boundingRectWithSize:CGSizeMake(KPLabelMaxW, KPLabelMaxH) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:_pLabel.font} context:nil].size;
_pLabel.frame = CGRectMake(20, 20, stringSize.width, stringSize.height);
_backView.frame = CGRectMake((KMainW - stringSize.width) * 0.5 - 20, (KMainH - stringSize.height) * 0.5 - 20, stringSize.width + 40, stringSize.height + 40);
[UIView animateWithDuration:0.2f animations:^{
_backView.alpha = 1.0f;
} completion:^(BOOL finished) {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)((duration - 0.4) * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[UIView animateWithDuration:0.2f animations:^{
_backView.alpha = 0.0f;
} completion:^(BOOL finished) {
[self dismiss];
}];
});
}];
}else {
self.pImageView.hidden = NO;
CGFloat imgViewW = pushing ? 200 : 60;
_pImageView.backgroundColor = pushing ? [UIColor clearColor] : [[UIColor blackColor] colorWithAlphaComponent:0.7f];
_pImageView.frame = CGRectMake((KMainW - imgViewW) * 0.5, (KMainH - imgViewW) * 0.5, imgViewW, imgViewW);
if (pushing) {
[self startPushingLoadingAnimation];
}else {
[self startLoadingAnimation];
}
}
});
}
//转圈加载动画
- (void)startLoadingAnimation
{
NSMutableArray *array = [NSMutableArray array];
for (int i = 0; i < 8; i++) {
NSString *imageName = [NSString stringWithFormat:@"com_loading%02d", i + 1];
UIImage *image = [UIImage imageNamed:imageName];
[array addObject:image];
}
[_pImageView setAnimationImages:array];
[_pImageView setAnimationDuration:0.6f];
[_pImageView startAnimating];
}
//空页面加载动画
- (void)startPushingLoadingAnimation
{
NSMutableArray *array = [NSMutableArray array];
for (int i = 0; i < 2; i++) {
NSString *imageName = [NSString stringWithFormat:@"loading_img%02d.jpg", i + 1];
UIImage *image = [UIImage imageNamed:imageName];
[array addObject:image];
}
[_pImageView setAnimationImages:array];
[_pImageView setAnimationDuration:0.4f];
[_pImageView startAnimating];
}
- (void)stopLoadingAnimation
{
[_pImageView stopAnimating];
[_pImageView performSelector:@selector(setAnimationImages:) withObject:nil afterDelay:0];
}
- (void)dismiss
{
[self stopLoadingAnimation];
NSMutableArray *windows = [[NSMutableArray alloc] initWithArray:[UIApplication sharedApplication].windows];
[windows removeObject:_pWindow];
_pWindow = nil;
}
- (UIWindow *)pWindow
{
if (!_pWindow) {
_pWindow = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
}
return _pWindow;
}
- (UILabel *)pLabel
{
if (!_pLabel) {
UILabel *pLabel = [[UILabel alloc] init];
pLabel.textColor = [UIColor whiteColor];
pLabel.backgroundColor = [UIColor clearColor];
pLabel.textAlignment = NSTextAlignmentCenter;
pLabel.font = [UIFont boldSystemFontOfSize:18.0f];
pLabel.numberOfLines = 0;
[self.backView addSubview:pLabel];
_pLabel = pLabel;
}
return _pLabel;
}
- (UIView *)backView
{
if (!_backView) {
UIView *backView = [[UIView alloc] init];
backView.alpha = 0.f;
backView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.7f];
backView.layer.cornerRadius = 3.f;
backView.layer.masksToBounds = YES;
[self addSubview:backView];
_backView = backView;
}
return _backView;
}
- (UIImageView *)pImageView
{
if (!_pImageView) {
UIImageView *pImageView = [[UIImageView alloc] init];
pImageView.hidden = YES;
pImageView.layer.cornerRadius = 3.f;
pImageView.layer.masksToBounds = YES;
[self addSubview:pImageView];
_pImageView = pImageView;
}
return _pImageView;
}
@end

HWProgress Demo 下载链接:http://code.cocoachina.com/view/134855

 

 

人已赞赏
iOS文章

iOS UIScrollView简介

2020-2-23 8:40:09

iOS文章

iOS 添加字体库

2020-2-23 9:18:48

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索