zoukankan      html  css  js  c++  java
  • UIWindow介绍

      1、作为容器,包含app所要显示的所有视图

      3、与UIViewController协同工作,方便完成设备方向旋转的支持

      1、addSubview

      2、rootViewController

    三、WindowLevel

     

        const UIWindowLevel UIWindowLevelNormal;
        const UIWindowLevel UIWindowLevelAlert;
        const UIWindowLevel UIWindowLevelStatusBar;
        typedef CGFloat UIWindowLevel;

    CGFloat _windowSublevel;),不过系统并没有把则个属性开出来。UIWindow的默认级别是UIWindowLevelNormal,我们打印输出这三个level的值分别如下:

    46:08.752 UIViewSample[395:f803] Normal window level: 0.000000  
    • 2012-03-27 22:46:08.754 UIViewSample[395:f803] Normal window level: 2000.000000  
    • 2012-03-27 22:46:08.755 UIViewSample[395:f803] Normal window level: 1000.000000  

      这样印证了他们级别的高低顺序从小到大为Normal < StatusBar < Alert,下面请看小的测试代码:

    TestWindowLevel
    复制代码
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    self.window.backgroundColor = [UIColor yellowColor];
    [self.window makeKeyAndVisible];

    UIWindow *normalWindow = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    normalWindow.backgroundColor = [UIColor blueColor];
    normalWindow.windowLevel = UIWindowLevelNormal;
    [normalWindow makeKeyAndVisible];

    CGRect windowRect = CGRectMake(50,
    50,
    [[UIScreen mainScreen] bounds].size.width - 100,
    [[UIScreen mainScreen] bounds].size.height - 100);
    UIWindow *alertLevelWindow = [[UIWindow alloc] initWithFrame:windowRect];
    alertLevelWindow.windowLevel = UIWindowLevelAlert;
    alertLevelWindow.backgroundColor = [UIColor redColor];
    [alertLevelWindow makeKeyAndVisible];

    UIWindow *statusLevelWindow = [[UIWindow alloc] initWithFrame:CGRectMake(0, 50, 320, 20)];
    statusLevelWindow.windowLevel = UIWindowLevelStatusBar;
    statusLevelWindow.backgroundColor = [UIColor blackColor];
    [statusLevelWindow makeKeyAndVisible];

    NSLog(@"Normal window level: %f", UIWindowLevelNormal);
    NSLog(@"Normal window level: %f", UIWindowLevelAlert);
    NSLog(@"Normal window level: %f", UIWindowLevelStatusBar);

    return YES;
    }
    复制代码

      1)我们生成的normalWindow虽然是在第一个默认的window之后调用makeKeyAndVisible,但是仍然没有显示出来。这说明当Level层级相同的时候,只有第一个设置为KeyWindow的显示出来,后面同级的再设置KeyWindow也不会显示。

      2)statusLevelWindow在alertLevelWindow之后调用makeKeyAndVisible,淡仍然只是显示在alertLevelWindow的下方。这说明UIWindow在显示的时候是不管KeyWindow是谁,都是Level优先的,即Level最高的始终显示在最前面。


    有时候,我们也希望在应用开发中,将某些界面覆盖在所有界面最上层。这个时候,我们就可以手工创建一个新的UIWindow。例如,想做一个密码保护功能,在用户从应用的任何界面按Home键退出,过段时间再从后台切换回来时,显示一个密码输入界面。

    Demo界面很简单,每次启动应用或者从后台进入应用,都会显示输入密码界面,只有密码输入正确,才能使用应用。

    技术分享

    大致代码如下:
    PasswordInputWindow.h 文件。   定义一个继承自UIWindow的子类 PasswordInputWindow,  shareInstance 是单例, show方法就是用来显示输入密码界面。
    @interface PasswordInputView : UIWindow
    
    + (PasswordInputView *)shareInstance;
    
    - (void)show;
    
    @end
    PasswordInputWindow.m 文件。
    #import "PasswordInputView.h"
    
    @interface PasswordInputView()
    @property (nonatomic,weak) UITextField *textField;
    @end
    
    @implementation PasswordInputView
    
    #pragma mark - Singleton
    + (PasswordInputView *)shareInstance {
        static id instance = nil;
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            instance = [[self alloc] initWithFrame:[UIScreen mainScreen].bounds];
        });
        
        return instance;
    }
    
    #pragma mark - Initilize
    - (instancetype)initWithFrame:(CGRect)frame {
        if (self = [super initWithFrame:frame]) {
            [self setup];
        }
        return self;
    }
    
    - (instancetype)initWithCoder:(NSCoder *)decoder {
        if (self = [super initWithCoder:decoder]) {
            [self setup];
        }
        return self;
    }
    
    - (void)setup {
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 50, 200, 20)];
        label.text = @"请输入密码";
        [self addSubview:label];
        
        UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 80, 200, 20)];
        textField.backgroundColor = [UIColor whiteColor];
        textField.secureTextEntry = YES;
        [self addSubview:textField];
        self.textField = textField;
        
        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(10, 110, 200, 44)];
        [button setBackgroundColor:[UIColor blueColor]];
        [button setTitle:@"确定" forState:UIControlStateNormal];
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(completeButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:button];
        
        self.backgroundColor = [UIColor yellowColor];
    }
    
    #pragma mark - Common Methods
    - (void)completeButtonPressed:(UIButton *)button {
        if ([self.textField.text isEqualToString:@"123456"]) {
            [self.textField resignFirstResponder];
            [self resignKeyWindow];
            self.hidden = YES;
        } else {
            [self showErrorAlertView];
        }
    }
    
    - (void)showErrorAlertView {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:@"密码错误,请重新输入" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alertView show];
    }
    
    - (void)show {
        [self makeKeyWindow];
        self.hidden = NO;
    }
    
    @end

    1.代码中我实现了initWithFrame和initWithCoder两个方法,这样可以保证,不管用户是纯代码还是xib实现的初始化,都没有问题。
    2.如果我们创建的UIWindow需 要处理键盘事件,那就要合理的将其设置为keyWindow。keyWindow是被系统设计用来接受键盘和其他非触摸事件的UIWindow。我们可以 通过makeKeyWindow 和 resignKeyWindow 方法来将自己创建的UIWindow实例设置成keyWindow。
    3. 加入以下代码,就可以保证,程序每次从后台进入的时候,先显示输入密码界面了。

    - (void)applicationDidBecomeActive:(UIApplication *)application {
        [[PasswordInputView shareInstance] show];
    }
  • 相关阅读:
    Oracle常用系统查询SQL
    easyui中使用的遮罩层
    EasyUI相同的Tab只打开一个(即EasyUI方法的调用方法)
    jQueryEasyUI创建菜单主页
    linux 的环境变量的配置文件
    angular reactive form
    svn代码回滚
    golang restful api
    golang embedded structs
    Angular Multiple HTTP Requests with RxJS
  • 原文地址:https://www.cnblogs.com/stephen-init/p/4739788.html
Copyright © 2011-2022 走看看