仿照支付客戶端退出后臺(tái)再次打開需要手勢解鎖

先看下效果圖(布局就隨便看看吧 ,重點(diǎn)是效果??)


finished3.gif

需求:上面的效果圖是在用戶從應(yīng)用程序的界面按下Home鍵退出,過一段時(shí)間再從后臺(tái)切換回來的時(shí)候,顯示一個(gè)密碼輸入的界面,只有當(dāng)用戶輸入了正確的密碼才能進(jìn)入退出前的界面.
需求分析:因?yàn)檫@個(gè)密碼輸入界面可能從任何應(yīng)用界面彈出,并且需要蓋在所有的界面上,所有它適合用一個(gè)UIWindow來實(shí)現(xiàn).代碼如下

1.創(chuàng)建一個(gè)PasswordInputWindow類繼承自UIWindow,在.h文件中實(shí)現(xiàn)兩個(gè)方法

@interface PasswordInputWindow : UIWindow

+(PasswordInputWindow *)sharedInstance;
- (void)show;

@end

2.在PasswordInputWindow類的.m文件中實(shí)現(xiàn)對應(yīng)的方法

+(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 setBackgroundColor:[UIColor blueColor]];
        button.titleLabel.textColor = [UIColor blackColor];
        [button setTitle:@"確定" forState:UIControlStateNormal];
        [button addTarget:self action:@selector(compleBtnPressed) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:button];
        self.backgroundColor = [UIColor yellowColor];
        self.textField = textField;
    }
    return self;
}
- (void)show{
    [self makeKeyWindow];
    self.hidden = NO;
}
- (void)compleBtnPressed{
    if ([self.textField.text isEqualToString:@"abcd"]) {
        [self.textField resignFirstResponder];
        [self resignKeyWindow];
        self.hidden = YES;
    }else{
        [self showErrorAlterView];
    }
}
- (void)showErrorAlterView{
    UIAlertView * view = [[UIAlertView alloc]initWithTitle:nil message:@"密碼錯(cuò)誤" delegate:nil cancelButtonTitle:@"ok" otherButtonTitles:nil, nil];
    [view show];
}

3.我們只需要在應(yīng)用進(jìn)入后臺(tái)的回調(diào)函數(shù)中,把自己的顯示出來即可.

//進(jìn)入后臺(tái)
- (void)applicationDidEnterBackground:(UIApplication *)application {
    [[PasswordInputWindow sharedInstance]show];
}

總結(jié):支付寶客戶端的手勢解鎖功能,應(yīng)用的啟動(dòng)介紹頁,應(yīng)用內(nèi)的彈窗廣告等都是利用的UIWindow一個(gè)很好的應(yīng)用.
參考資料:ios開發(fā)進(jìn)階(唐巧)

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 1.ios高性能編程 (1).內(nèi)層 最小的內(nèi)層平均值和峰值(2).耗電量 高效的算法和數(shù)據(jù)結(jié)構(gòu)(3).初始化時(shí)...
    歐辰_OSR閱讀 30,286評(píng)論 8 265
  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,351評(píng)論 25 708
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,697評(píng)論 19 139
  • 活著 當(dāng)夜色充斥整個(gè)世界, 我在想, 活著, 未開的花,未舒的葉。 當(dāng)風(fēng)撫摸大地的時(shí)候, ...
    笈荼閱讀 276評(píng)論 0 2
  • 夜里,和她爭吵 無理的指責(zé) 她哭了 我流淚了 大約在凌晨12點(diǎn)的街道 我獨(dú)自走著 好大的馬路 好大的城市 好大的夜...
    陳浮點(diǎn)閱讀 187評(píng)論 0 1

友情鏈接更多精彩內(nèi)容