iOS不得姐項目--封裝狀態列指標(UIWindow實現)

來源:互聯網
上載者:User

標籤:

一.標頭檔

 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實現)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.