UIWindow

 通常一个程序中只会有一个UIWindow,但有些时候我们调用系统的控件(例如UIAlertView)时,iOS系统为了保证UIAlertView在所有的界面之上,它会临时创建一个新的UIWindow,通过将其UIWindowUIWindowLevel设置的更高,让UIAlertView盖在所有的应用界面之上。


UIWindow和UIView不同,UIWindow一旦被创建,它就自动被添加到整个界面上了。


 UIWindowLevel系统一共有三种取值,UIWindowLevelNormal = 0 UIWindowLevelAlert = 1000 UIWindowLevelStatuBar = 2000。默认程序的UIWindow的层级是UIWindowLevelNormal。在实际应用中,WindowLevel的取值并不限于上面提到的三个值(WindowLevel可以为正为负为零)


 支付宝客户端的手势解锁功能,也是UIWindow的一个极好的应用。除了密码输入界面外,其他适合用UIWindow来实现的功能还包括:应用的启动介绍页,应用内的通知提醒显示,应用内的弹窗广告等。



#import <UIKit/UIKit.h>


@interface PasswordInputWindow : UIWindow


+ (PasswordInputWindow *)sharedInstance;


- (void)show;


@end



#import "PasswordInputWindow.h"


@interface PasswordInputWindow ()

{

    UITextField *_textField;

}


@end


@implementation PasswordInputWindow



+ (PasswordInputWindow *)sharedInstance

{

    static id sharedInstance = nil;

    static dispatch_once_t onceToken;

    dispatch_once(&onceToken, ^{

        sharedInstance = [[self alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    });

    return sharedInstance;

}


- (id)initWithFrame:(CGRect)frame

{

    self = [super initWithFrame:frame];

    if (self) {

        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];

        

        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(10, 110, 200, 44)];

        button.backgroundColor = [UIColor blueColor];

        button.titleLabel.textColor = [UIColor blackColor];

        [button setTitle:@"确定" forState:UIControlStateNormal];

        [button addTarget:self action:@selector(completeButtonPressed:) forControlEvents:UIControlEventTouchUpInside];

        [self addSubview:button];

        

        self.backgroundColor = [UIColor yellowColor];

        _textField = textField;

    }

    return self;

}


- (void)show

{

    // 如果我们创建的UIWindow需要处理键盘事件,那就需要合理地将其设置为keyWindowkeyWindow是被系统设计用来接收键盘和其他非触摸事件的UIWindow。我们可以通过makeKeyWindowresignKeyWindow方法来将自己创建的UIWindow实例设置成keyWindow

    [self makeKeyWindow];

    self.hidden = NO;

}


- (void)completeButtonPressed:(UIButton *)btn

{

    if ([_textField.text isEqualToString:@"abcd"]) {

        [_textField resignFirstResponder];

        [self resignKeyWindow];

        self.hidden = YES;

    } else {

        [self showErrorAlertView];

    }

}


- (void)showErrorAlertView

{

    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:@"密码错误,正确密码是abcd" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];

    [alertView show];

}



// AppDelegate.m文件

- (void)applicationDidEnterBackground:(UIApplication *)application

{

    [[PasswordInputWindow sharedInstance] show];

}



你可能感兴趣的:(ios,UIWindow)