//
// LSProgressHUD.h
// SoWeChat
//
// Created by linpeng on 14-8-12.
// Copyright (c) 2014年 linpeng. All rights reserved.
//
#import <UIKit/UIKit.h>
#define LSProgressHUDICON_Default @"LSProgressHUD"
#define LSProgressHUDICON_Fail @"LSProgressHUD_Error"
#define LSProgressHUDICON_Suc @"LSProgressHUD_Suc"
typedef void(^LSProgressHUDBlock)(BOOL finish);
typedef enum
{
LSProgressHUD_Wait = 0,
LSProgressHUD_Succces = 1,
LSProgressHUD_Fail = 2,
LSProgressHUD_Default = LSProgressHUD_Wait,
}LSProgressHUDType;
@interface LSProgressHUD : UIView
/**
* 菊花展示
*
* @param superview 父视图
* @param msg 提示文字
* @param doBlock 异步执行的操作
*/
+(void)showInSuperView:(UIView *)superview message:(NSString *)msg whileExecutingBlock:(LSProgressHUDBlock)doBlock;
/**
* 操作成功
*
* @param superview 父视图
* @param msg 提示文字
*/
+(void)showSuccessInSuperView:(UIView *)superview message:(NSString *)msg;
/**
* 操作失败
*
* @param superview 父视图
* @param msg 提示文字
*/
+(void)showFailInSuperView:(UIView *)superview message:(NSString *)msg;
@end
=======================================
//
// LSProgressHUD.m
// SoWeChat
//
// Created by linpeng on 14-8-12.
// Copyright (c) 2014年 linpeng. All rights reserved.
//
#import "LSProgressHUD.h"
#define RGBCOLOR(r,g,b) [UIColor colorWithRed:(r)/255.0f green:(g)/255.0f blue:(b)/255.0f alpha:1]
#define HUD_COLOR RGBCOLOR(86,206,186)
@interface LSProgressHUD()
@property(nonatomic,strong)UILabel *labMsg;
@property(nonatomic,strong)UIImageView *icon;
@end
@implementation LSProgressHUD
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
}
return self;
}
-(id)initWithType:(LSProgressHUDType)type
{
if (self=[super init]) {
int w = [UIScreen mainScreen].bounds.size.width;
int h = [UIScreen mainScreen].bounds.size.height;
[self setFrame:CGRectMake(0, 0, w, h)];
[self setBackgroundColor:[UIColor clearColor]];
UIView *bg = [[UIView alloc] initWithFrame:CGRectMake(0.2*w,(h-0.2*h)/2, 0.6*w, 100)];
[bg setBackgroundColor:[HUD_COLOR colorWithAlphaComponent:1]];
bg.layer.cornerRadius = 10;
bg.layer.borderWidth = 0.5;
bg.layer.borderColor = HUD_COLOR.CGColor;
bg.layer.masksToBounds = YES;
[self addSubview:bg];
self.icon = [[UIImageView alloc] initWithFrame:CGRectMake((bg.size.width-40)/2, (bg.size.height-60)/2, 40, 40)];
[_icon setImage:[UIImage imageNamed:LSProgressHUDICON_Default]];
[bg addSubview:_icon];
self.labMsg = [[UILabel alloc] initWithFrame:CGRectMake(0, _icon.origin.y+_icon.size.height+10, bg.size.width, 20)];
[_labMsg setBackgroundColor:[UIColor clearColor]];
[_labMsg setFont:[UIFont systemFontOfSize:15]];
[_labMsg setTextAlignment:NSTextAlignmentCenter];
[_labMsg setTextColor:[UIColor whiteColor]];
[bg addSubview:_labMsg];
if (type==LSProgressHUD_Wait) {
[bg setBackgroundColor:[UIColor whiteColor]];
[_labMsg setTextColor:HUD_COLOR];
[bg setBackgroundColor:[HUD_COLOR colorWithAlphaComponent:0.1]];
//旋转
CABasicAnimation* rotationAnimation;
rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 2.0 ];
rotationAnimation.duration = 1;
rotationAnimation.cumulative = YES;
rotationAnimation.repeatCount = HUGE_VAL;
[_icon.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
}
}
return self;
}
+(void)showInSuperView:(UIView *)superview message:(NSString *)msg whileExecutingBlock:(LSProgressHUDBlock)doBlock
{
LSProgressHUD *HUD = [[LSProgressHUD alloc] initWithType:LSProgressHUD_Default];
HUD.labMsg.text = msg;
[superview addSubview:HUD];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void){
doBlock(YES);
dispatch_async(dispatch_get_main_queue(), ^{
[UIView animateWithDuration:0.5 animations:^{
HUD.alpha = 0;
}completion:^(BOOL finished) {
[HUD removeFromSuperview];
}];
});
});
}
+(void)showSuccessInSuperView:(UIView *)superview message:(NSString *)msg
{
LSProgressHUD *HUD = [[LSProgressHUD alloc] initWithType:LSProgressHUD_Succces];
HUD.labMsg.text = msg;
[HUD.icon setImage:[UIImage imageNamed:LSProgressHUDICON_Suc]];
HUD.alpha = 0;
[UIView animateWithDuration:1 animations:^{
HUD.alpha = 1;
}completion:^(BOOL finished) {
[superview addSubview:HUD];
}];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[UIView animateWithDuration:0.5 animations:^{
HUD.alpha = 0;
}completion:^(BOOL finished) {
[HUD removeFromSuperview];
}];
});
}
+(void)showFailInSuperView:(UIView *)superview message:(NSString *)msg
{
LSProgressHUD *HUD = [[LSProgressHUD alloc] initWithType:LSProgressHUD_Succces];
HUD.labMsg.text = msg;
[HUD.icon setImage:[UIImage imageNamed:LSProgressHUDICON_Fail]];
HUD.alpha = 0;
[UIView animateWithDuration:1 animations:^{
HUD.alpha = 1;
}completion:^(BOOL finished) {
[superview addSubview:HUD];
}];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[UIView animateWithDuration:0.5 animations:^{
HUD.alpha = 0;
}completion:^(BOOL finished) {
[HUD removeFromSuperview];
}];
});
}
@end
调用:
[LSProgressHUDshowInSuperView:self.viewmessage:@"请稍等"whileExecutingBlock:^(BOOL finish) {
sleep(5);
}];
LSProgressHUD *HUD = [[LSProgressHUDalloc] initWithType:LSProgressHUD_Default];
HUD.labMsg.text = msg;
[superviewaddSubview:HUD];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^(void){
doBlock(YES);
dispatch_async(dispatch_get_main_queue(), ^{
[UIViewanimateWithDuration:0.5animations:^{
HUD.alpha =0;
}completion:^(BOOL finished) {
[HUDremoveFromSuperview];
}];
});
});