关于UIWindowLevel
定义了UIWindow的层级,系统有3个取值
UIKIT_EXTERN const UIWindowLevel UIWindowLevelNormal;
UIKIT_EXTERN const UIWindowLevel UIWindowLevelAlert;
UIKIT_EXTERN const UIWindowLevel UIWindowLevelStatusBar;
NSLog(@"UIWindowLevelNormal:%f,UIWindowLevelAlert:%f,UIWindowLevelStatusBar:%f",UIWindowLevelNormal,UIWindowLevelAlert,UIWindowLevelStatusBar);
output:
UIWindowLevelNormal:0.000000,UIWindowLevelAlert:2000.000000,UIWindowLevelStatusBar:1000.000000;
系统默认层级是UIWindowLevelNormal,如果有UIAlertView,会创建层级为UIWindowLevelAlert的UIWindow,其UIWindowLevel值更高,覆盖在上面,实际应用中,UIWindowLevel值不止这3个;
手动创建UIWindow,区别于UIView,UIWindow一旦被创建,将自动地添加到整个界面上~
手动创建UIWindow
#import "ViewController.h"
@interface ViewController ()
{
UIWindow *_myWindow;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor redColor];
//创建Button,点击创建UIWindow
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 50)];
[button setTitle:@"创建window" forState:UIControlStateNormal];
[button addTarget:self action:@selector(_creatAction:) forControlEvents:UIControlEventTouchUpInside];
button.center = self.view.center;
[self.view addSubview:button];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)_creatAction:(UIButton *)btn {
//创建一个UIWindow
_myWindow = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
_myWindow.windowLevel = UIWindowLevelNormal;
_myWindow.backgroundColor = [UIColor blackColor];
_myWindow.alpha = 0.7;
_myWindow.hidden = NO;
//添加手势
UIGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] init];
[gesture addTarget:self action:@selector(hideWindow:)];
[_myWindow addGestureRecognizer:gesture];
// 在window上添加一个UILabel
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 200, 20)];
label.text = @"LABEL";
[label setTextColor:[UIColor whiteColor]];
[_myWindow addSubview:label];
}
- (void)hideWindow:(UIGestureRecognizer *)gesture {
_myWindow.hidden = YES;
_myWindow = nil;
}
注意:
1、UIWindow不可滥用理应由ViewController实现的效果,就应该用ViewController addSubview来实现;
2、不可将需要弹出的界面都设置为单例,需要时调用显示,这会使得新创建的UIWindow一直得不到释放,出现多个UIWindow需要下过户有层级覆盖关系式,实现起来会比较复杂
(单例模式的意思就是只有一个实例。单例模式确保某一个类只有一个实例,而且自行实例化并向整个系统提供这个实例。这个类称为单例类。)
以上为阅读《iOS开发进阶》第十二章摘记及练习
版权声明:本文为博主原创文章,未经博主允许不得转载。
时间: 2024-11-05 22:38:33