1. 程式人生 > >iOS 開發實戰-鎖屏介面(密碼解鎖)

iOS 開發實戰-鎖屏介面(密碼解鎖)

改篇是介紹一個類似於支付寶返回Home重新開啟後手勢解鎖,不同的是改篇介紹的解鎖方法是輸入密碼,而不是手勢解鎖,下次有機會在單獨寫一篇手勢解鎖的實戰介紹。

涉及到的知識點

  • UIWindow
  • AutoLayout
  • UIButton,UITextField
  • AppDelegate

開發

基本思路:

當按下Home按鈕時,App進入後臺,在進入後臺的程式碼出將鎖屏介面Show出來,解鎖成功後,鎖屏介面消失。這裡的鎖屏介面我用UIWindow來編寫。

編寫UIWidow鎖屏介面

1.首先新建一個繼承UIWindow的類,我在這個專案中取名為LockScreen。

2.LockScreen設計成一個單例模型。

.h檔案中新增兩個方法,單例和show。

#LockScreen.h
@interface LockScreen : UIWindow
+(id)shareInstance;
-(void)show;
@end

初始化程式碼

#LockScreen.m
+(id)shareInstance
{
    static dispatch_once_t  once;
    static LockScreen * unlockWindow = nil;
    dispatch_once(&once, ^{
        unlockWindow = [[LockScreen alloc] initWithFrame:[UIScreen mainScreen].bounds];
    });
    return unlockWindow;
}

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.windowLevel = UIWindowLevelAlert;
        [self setBackgroundColor:[UIColor groupTableViewBackgroundColor]];
        [self addSubview:self.passwordField];
        [self addSubview:self.doneButton];
    }
    return self;
}

上面程式碼中,有一個windowLevel的屬性,這個是設定UIWindow所在的層級,系統提供了三種層級,也可以自定義層級,因為改值是一個long型別的

UIKIT_EXTERN const UIWindowLevel UIWindowLevelNormal;  //最下層

UIKIT_EXTERN const UIWindowLevel UIWindowLevelAlert;   //最上層

UIKIT_EXTERN const UIWindowLevel UIWindowLevelStatusBar;  //中間層

3.新增兩個控制元件(UITextField,UIButton)

-(UITextField *)passwordField
{
    if (!_passwordField) {
        _passwordField = [[UITextField alloc]init];
        _passwordField.secureTextEntry = YES;
        _passwordField.placeholder = @"請輸入密碼";
        _passwordField.backgroundColor = [UIColor whiteColor];
    }
    return _passwordField;
}

-(UIButton *)doneButton
{
    if (!_doneButton) {
        _doneButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        [_doneButton addTarget:self
                        action:@selector(tapButton:)
              forControlEvents:UIControlEventTouchUpInside];
        [_doneButton setTitle:@"確定" forState:UIControlStateNormal];
        [_doneButton setBackgroundColor:[UIColor lightGrayColor]];
    }
    return _doneButton;
}
#pragma mark - button status
-(IBAction)tapButton:(id)sender
{
    if ([self.passwordField.text isEqualToString:@"abc123"]) {
        
        [self resignKeyWindow];
        self.hidden = YES;
    }
    else
    {
        UIAlertView * alter = [[UIAlertView alloc] initWithTitle:@"Warning" message:@"password error" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alter show];
    }
}
如果密碼輸錯,則彈出警告框。

4.給控制元件設定constrain而不是使用setFrame

當然也可以用setFrame來定義控制元件的尺寸,但是autolayout能更好的適配不同尺寸的螢幕。關於autolayout的介紹請戳這裡

#pragma mark - text field delegate
-(void)configConstrain
{
    UITextField * passwordTextField = self.passwordField;
    UIButton * doneButton = self.doneButton;
    passwordTextField.translatesAutoresizingMaskIntoConstraints = NO;
    doneButton.translatesAutoresizingMaskIntoConstraints = NO;
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-50-[passwordTextField]-50-|"
                                                                options:0
                                                                metrics:nil
                                                                  views:NSDictionaryOfVariableBindings(passwordTextField)]];
    
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-100-[doneButton]-100-|"
                                                                 options:0
                                                                 metrics:nil
                                                                   views:NSDictionaryOfVariableBindings(doneButton)]];
    CGFloat middleHeight = [UIScreen mainScreen].bounds.size.height/2;
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[passwordTextField(50)]-30-[doneButton(50)]-middleHeight-|" options:0 metrics:@{@"middleHeight":@(middleHeight)} views:NSDictionaryOfVariableBindings(passwordTextField,doneButton)]];
    
}

5.show LockScreen

-(void)show
{
    [self makeKeyWindow];
    [self configConstrain];
    self.hidden = NO;
}
程式碼中makeKeyWindow是將該UIWindow設定為鍵盤可響應的狀態。

在AppDelegate.m中新增程式碼

5.在AppDelegate.m中新增程式碼

- (void)applicationDidEnterBackground:(UIApplication *)application {
    [[LockScreen shareInstance] show];
}
程式進入後臺時,將鎖屏介面show出來。

6.Bingo,執行^^

後記

UIWindow是UIView的子類,所以可以使用UIView中的方法,例如:addSubview等。

這次是密碼鎖屏介面,後面會在寫一篇手勢解鎖的文章,該文章的原始碼在github上。