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

}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值