偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

iOS自定義狀態(tài)欄代碼解析

移動開發(fā) iOS
默認(rèn)我們的UIView layer都是在UIWindowLevelNormal上,這也就是為什么系統(tǒng)彈出來的對話框在我們的視圖之上,因為它的Window Level級別更高。

公司的開發(fā)的項目要求在狀態(tài)欄上邊加入程序下載的進(jìn)度條,之前寫的程序,由于是根據(jù)ipad的朝向來設(shè)置自定義的狀態(tài)欄的frame,以及子視圖的 frame和transform,出現(xiàn)一些不太容易解決的bug。這兩天正好項目不太緊,就好好學(xué)習(xí)一下這方面的知識,以下是我所總結(jié)的一點經(jīng)驗:

這里說明一下,Apple沒有開放的狀態(tài)欄的API,在ios 的官方文檔沒有提到修改Window Level的方式;

先看一下Window Level的可用的值包括:

 1: typedef CGFloat UIWindowLevel;
 2: const UIWindowLevel UIWindowLevelNormal; // 0.0
 3: const UIWindowLevel UIWindowLevelAlert; // 2000.0
 4: const UIWindowLevel UIWindowLevelStatusBar; // 1000.0

默認(rèn)我們的UIView layer都是在UIWindowLevelNormal上,這也就是為什么系統(tǒng)彈出來的對話框在我們的視圖之上,因為它的Window Level級別更高。

根據(jù)WindowLevel的原理我們也就知道,如果想在系統(tǒng)的狀態(tài)欄上,添加自定義的狀態(tài)欄,就需要比UIWindowLevelStatusBar的級別更高,接下來,用代碼說明一下:

首先,先建一個Single View Application,名字自定義就可以了,

然后,新建一個類命名為: StatusBarOverlay 繼承自UIWindow類,代碼:

StatusBarOverlay.h文件

 1: #import <Foundation/Foundation.h>
 2:  
 3: @interface StatusBarOverlay : UIWindow{
 4:  UIView *contentView;
 5:  UILabel *textLabel;
 6: }
 7:  
 8: @property (nonatomic, retain) UIView *contentView;
 9:  
 10: @property (nonatomic, retain) UILabel *textLabel;
 11:  
 12: @end

StatusBarOverlay.m文件

 1: //
 2: //  StatusBarOverlay.m
 3: //  StatusBarDemo
 4: //
 5: //  Created by jordy wang on 12-8-7.
 6: //  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
 7: //
 8:  
 9: #import "StatusBarOverlay.h"
 10:  
 11: #define STATUS_BAR_ORIENTATION [UIApplication sharedApplication].statusBarOrientation
 12: #define ROTATION_ANIMATION_DURATION [UIApplication sharedApplication].statusBarOrientationAnimationDuration
 13:  
 14:  
 15: @interface StatusBarOverlay()
 16:  
 17: - (void)initializeToDefaultState;
 18: - (void)rotateStatusBarWithFrame:(NSValue *)frameValue;
 19: - (void)setSubViewHFrame;
 20: - (void)setSubViewVFrame;
 21: @end
 22:  
 23:  
 24: @implementation StatusBarOverlay
 25: @synthesize contentView;
 26: @synthesize textLabel;
 27:  
 28: //重寫init方法
 29: - (id)init
 30: {
 31:  self = [super initWithFrame:CGRectZero];
 32:  if (self) {
 33:  self.windowLevel = UIWindowLevelStatusBar + 1;
 34:  self.frame = [UIApplication sharedApplication].statusBarFrame;
 35:  [self setBackgroundColor:[UIColor orangeColor]];
 36:  [self setHidden:NO];
 37: 
 38:  //內(nèi)容視圖
 39:  UIView *_contentView = [[UIView alloc] initWithFrame:self.bounds];
 40:  self.contentView = _contentView;
 41:  [self.contentView setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
 42:  [self.contentView setBackgroundColor:[UIColor cyanColor]];
 43:  [self addSubview:self.contentView];
 44:  [_contentView release];
 45: 
 46: 
 47:  //添加textLabel
 48:  UILabel *_textLabel = [[UILabel alloc] initWithFrame:CGRectMake(30, 0, CGRectGetWidth(self.frame)-60, CGRectGetHeight(self.frame))];
 49:  self.textLabel = _textLabel;
 50:  [self.textLabel setBackgroundColor:[UIColor blueColor]];
 51:  [self.textLabel setFont:[UIFont systemFontOfSize:12]];
 52:  [self.textLabel setTextAlignment:UITextAlignmentCenter];
 53:  [self.textLabel setTextColor:[UIColor blackColor]];
 54:  [self.textLabel setText:@"自定義的狀態(tài)欄 author by jordy"];
 55:  [self.contentView addSubview:self.textLabel];
 56:  [_textLabel release];
 57: 
 58:  //注冊監(jiān)聽---當(dāng)屏幕將要轉(zhuǎn)動時,所出發(fā)的事件(用于操作本視圖改變其frame)
 59:  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(willRotateScreenEvent:) 

name:UIApplicationWillChangeStatusBarFrameNotification object:nil];
 60:  //初始化
 61:  [self initializeToDefaultState];
 62:  }
 63: 
 64:  return self;
 65: }
 66:  
 67:  
 68:  
 69:  
 70: //初始化為默認(rèn)狀態(tài)
 71: - (void)initializeToDefaultState
 72: {
 73:  //獲取當(dāng)前的狀態(tài)欄位置
 74:  CGRect statusBarFrame = [UIApplication sharedApplication].statusBarFrame;
 75:  //設(shè)置當(dāng)前視圖的旋轉(zhuǎn), 根據(jù)當(dāng)前設(shè)備的朝向
 76:  [self rotateStatusBarWithFrame:[NSValue valueWithCGRect:statusBarFrame]];
 77: 
 78: 
 79: 
 80: }
 81:  
 82:  
 83: //旋轉(zhuǎn)屏幕
 84: - (void)rotateStatusBarWithFrame:(NSValue *)frameValue
 85: {
 86:  CGRect frame = [frameValue CGRectValue];
 87:  UIInterfaceOrientation orientation = STATUS_BAR_ORIENTATION;
 88: 
 89:  if (orientation == UIDeviceOrientationPortrait) {
 90:  self.transform = CGAffineTransformIdentity; //屏幕不旋轉(zhuǎn)
 91:  [self setSubViewVFrame];
 92:  }else if (orientation == UIDeviceOrientationPortraitUpsideDown) {
 93:  self.transform = CGAffineTransformMakeRotation(M_PI); //屏幕旋轉(zhuǎn)180度
 94:  [self setSubViewVFrame];
 95:  }else if (orientation == UIDeviceOrientationLandscapeRight) {
 96:  self.transform = CGAffineTransformMakeRotation((M_PI * (-90.0f) / 180.0f)); //屏幕旋轉(zhuǎn)-90度
 97:  [self setSubViewHFrame];
 98:  }else if (orientation == UIDeviceOrientationLandscapeLeft){
 99:  self.transform = CGAffineTransformMakeRotation(M_PI * 90.0f / 180.0f); //屏幕旋轉(zhuǎn)90度
 100:  [self setSubViewHFrame];
 101:  }
 102: 
 103:  self.frame = frame;
 104:  [self.contentView setFrame:self.bounds];
 105: }
 106:  
 107: //設(shè)置橫屏的子視圖的frame
 108: - (void)setSubViewHFrame
 109: {
 110:  self.textLabel.frame = CGRectMake(30, 0, 1024-60, 20);
 111: }
 112: //設(shè)置豎屏的子視圖的frame
 113: - (void)setSubViewVFrame
 114: {
 115:  self.textLabel.frame = CGRectMake(30, 0, 748-60, 20);
 116: }
 117:  
 118: #pragma mark -
 119: #pragma mark 響應(yīng)屏幕即將旋轉(zhuǎn)時的事件響應(yīng)
 120: - (void)willRotateScreenEvent:(NSNotification *)notification
 121: {
 122:  NSValue *frameValue = [notification.userInfo valueForKey:UIApplicationStatusBarFrameUserInfoKey];
 123:  [self rotateStatusBarAnimatedWithFrame:frameValue];
 124: }
 125:  
 126: - (void)rotateStatusBarAnimatedWithFrame:(NSValue *)frameValue {
 127:  [UIView animateWithDuration:ROTATION_ANIMATION_DURATION animations:^{
 128:  self.alpha = 0;
 129:  } completion:^(BOOL finished) {
 130:  [self rotateStatusBarWithFrame:frameValue];
 131:  [UIView animateWithDuration:ROTATION_ANIMATION_DURATION animations:^{
 132:  self.alpha = 1;
 133:  }];
 134:  }];
 135: }
 136:  
 137: - (void)dealloc
 138: {
 139:  [[NSNotificationCenter defaultCenter] removeObserver:self];
 140:  [textLabel release];
 141:  textLabel = nil;
 142: 
 143:  [contentView release];
 144:  contentView = nil;
 145: 
 146:  [super dealloc];
 147: }
 148:  
 149: @end

由于代碼比較簡單,并且我在上述代碼里有相應(yīng)的注釋,這里需要說明一點的是,默認(rèn)我們繼承自UIWindow的StatusBarOverlay類是hidden狀態(tài),需要在初始化的時候設(shè)置它的hidden屬性為NO,

在屏幕旋轉(zhuǎn)過程中,自定義的狀態(tài)欄與UIViewController之間的旋轉(zhuǎn)是分離的,所以我們需要做一個隱藏的動畫,在旋轉(zhuǎn)過程前先隱藏自定義的狀態(tài)欄,旋轉(zhuǎn)結(jié)果后設(shè)置顯示狀態(tài)。

如果需要做一種動畫,比方從底部下移顯示一條信息,隔N秒后又自動收回的動畫,直接設(shè)置自定義的視圖的y坐標(biāo)就可以了,默認(rèn)y坐標(biāo)設(shè)置是0。

最后, 使用它的方式也比較簡單,只需要初始化,代碼:

StatusBarOverlay *statusBarOverlay = [[StatusBarOverlay alloc] init];

由于我公司的需求是開機(jī)自動下載的功能,所以我在初始化的時候,是放在了AppDelegate中。

責(zé)任編輯:冰凝兒
相關(guān)推薦

2013-07-18 16:09:10

自定義iOS狀態(tài)欄iOS開發(fā)iOS學(xué)習(xí)

2014-06-06 14:03:13

iOS狀態(tài)欄提示控件原理

2017-10-25 14:07:54

APPiOSxcode

2021-08-24 15:25:59

鴻蒙HarmonyOS應(yīng)用

2013-11-20 15:08:32

iOS開發(fā)狀態(tài)欄

2017-02-17 11:00:57

狀態(tài)欄Android

2013-06-27 11:10:01

iOS開發(fā)自定義UISlider

2014-12-10 10:37:45

Android自定義布局

2016-11-29 11:20:08

Android

2015-02-12 14:49:36

CGToast狀態(tài)欄提示Status

2012-06-01 11:02:33

2011-08-02 11:17:13

iOS開發(fā) View

2021-01-20 08:58:39

iOS 14桌面圖標(biāo)快捷指令

2013-05-30 15:53:17

iOS開發(fā)iOS SDKPopver

2022-02-21 15:16:30

HarmonyOS鴻蒙操作系統(tǒng)

2011-05-04 10:40:02

網(wǎng)頁加載進(jìn)度標(biāo)題欄lephone

2011-04-27 10:31:38

Java

2012-04-16 14:32:31

iOS選擇器代碼

2015-02-12 15:33:43

微信SDK

2015-01-15 16:45:05

iOS源碼自定義畫圖
點贊
收藏

51CTO技術(shù)棧公眾號