码迷,mamicode.com
首页 > 移动开发 > 详细

iOS不得姐项目--封装状态栏指示器(UIWindow实现)

时间:2016-05-24 19:04:25      阅读:204      评论:0      收藏:0      [点我收藏+]

标签:

一.头文件

 1 #import <UIKit/UIKit.h>
 2 
 3 @interface ChaosStatusBarHUD : NSObject
 4 /** 显示成功信息 */
 5 + (void)showSuccess:(NSString *)str;
 6 /** 显示失败信息 */
 7 + (void)showError:(NSString *)str;
 8 /** 显示正在加载的信息 */
 9 + (void)showLoading:(NSString *)str;
10 /** 隐藏 */
11 + (void)hide;
12 /** 显示纯文本 */
13 + (void)showMessage:(NSString *)str;
14 /** 自定义图标 文字 */
15 + (void)showMessage:(NSString *)str withImage:(UIImage *)image;
16 @end

二.实现.m文件

  1 #import "ChaosStatusBarHUD.h"
  2 
  3 #define ChaosWindowH 20
  4 #define ChaosScreenW [UIScreen mainScreen].bounds.size.width
  5 
  6 @implementation ChaosStatusBarHUD
  7 
  8 static NSTimer *timer_;
  9 static UIWindow *window_;
 10 
 11 
 12 + (void)showWindow
 13 {
 14     // 首先结束之前的定时器
 15     [timer_ invalidate];
 16     UIWindow *window = [[UIWindow alloc] init];
 17     window.backgroundColor = [UIColor blackColor];
 18     window.windowLevel = UIWindowLevelStatusBar;
 19     window.hidden = NO;
 20     window_ = window;
 21     window.frame = CGRectMake(0, -ChaosWindowH, ChaosScreenW, ChaosWindowH);
 22     
 23     // 动画效果
 24     [UIView animateWithDuration:0.25 animations:^{
 25         
 26         window.frame = CGRectMake(0, 0, ChaosScreenW, ChaosWindowH);
 27     }];
 28 }
 29 /** 自定义图标 文字 */
 30 + (void)showMessage:(NSString *)str withImage:(UIImage *)image
 31 {
 32     // 创建窗体
 33     [self showWindow];
 34     // 添加按钮
 35     UIButton *button = [[UIButton alloc] init];
 36     button.frame = window_.bounds;
 37     [button setImage:image forState:UIControlStateNormal];
 38     [button setTitle:str forState:UIControlStateNormal];
 39     button.titleEdgeInsets = UIEdgeInsetsMake(0, 20, 0, 0);
 40     button.titleLabel.font = [UIFont systemFontOfSize:13];
 41     [window_ addSubview:button];
 42     
 43     // 两秒后结束动画
 44     timer_ = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(hide) userInfo:nil repeats:NO];
 45 }
 46 
 47 /** 显示纯文本 */
 48 + (void)showMessage:(NSString *)str
 49 {
 50     [self showMessage:str withImage:nil];
 51 }
 52 
 53 /** 显示成功信息 */
 54 + (void)showSuccess:(NSString *)str
 55 {
 56     [self showMessage:str withImage:[UIImage imageNamed:@"success"]];
 57 }
 58 
 59 /** 显示失败信息 */
 60 + (void)showError:(NSString *)str
 61 {
 62     [self showMessage:str withImage:[UIImage imageNamed:@"error"]];
 63 }
 64 
 65 /** 显示正在加载的信息 */
 66 + (void)showLoading:(NSString *)str
 67 {
 68     // 停止定时器
 69     [timer_ invalidate];
 70     timer_ = nil;
 71     // 创建窗口
 72     [self showWindow];
 73     // 添加按钮
 74     UIButton *button = [[UIButton alloc] init];
 75     button.frame = window_.bounds;
 76     [button setTitle:str forState:UIControlStateNormal];
 77     button.titleLabel.font = [UIFont systemFontOfSize:13];
 78     [window_ addSubview:button];
 79     // 计算按钮文字宽度
 80     CGFloat titleWidth = [str sizeWithAttributes:@{NSFontAttributeName : [UIFont systemFontOfSize:13]}].width;
 81     // 计算菊花的X值
 82     CGFloat x = (ChaosScreenW - 2 * titleWidth) * 0.5;
 83     // 计算loadingView的Y值
 84     CGFloat y = window_.frame.size.height * 0.5;
 85     UIActivityIndicatorView *loadingView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
 86     [loadingView startAnimating];
 87     [window_ addSubview:loadingView];
 88     loadingView.center = CGPointMake(x, y);
 89 }
 90 
 91 /** 隐藏 */
 92 + (void)hide
 93 {
 94     // 动画效果
 95     [UIView animateWithDuration:0.25 animations:^{
 96         
 97         window_.frame = CGRectMake(0, -20, ChaosScreenW, ChaosWindowH);
 98     } completion:^(BOOL finished) {
 99         
100         timer_ = nil;
101         window_ = nil;
102     }];
103 }
104 @end

 

iOS不得姐项目--封装状态栏指示器(UIWindow实现)

标签:

原文地址:http://www.cnblogs.com/gchlcc/p/5524225.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!