bc... [密码]
Home » Source Code » » LLLockViewController.m

LLLockViewController.m ( File view )

From:
Description:...
  • By lzwandnju 2017-02-09
  • View(s):0
  • Download(s):0
  • Point(s): 1
			//
//  LLLockViewController.m
//  LockSample
//
//  Created by Lugede on 14/11/11.
//  Copyright (c) 2014年 lugede.cn. All rights reserved.
//

#import "LLLockViewController.h"
#import "LLLockIndicator.h"

#define kTipColorNormal [UIColor blackColor]
#define kTipColorError [UIColor redColor]


@interface LLLockViewController ()
{

    int nRetryTimesRemain; // 剩余几次输入机会

}

@property (weak, nonatomic) IBOutlet UIImageView *preSnapImageView; // 上一界面截图
@property (weak, nonatomic) IBOutlet UIImageView *currentSnapImageView; // 当前界面截图
@property (nonatomic, strong) IBOutlet LLLockIndicator* indecator; // 九点指示图
@property (nonatomic, strong) IBOutlet LLLockView* lockview; // 触摸田字控件
@property (strong, nonatomic) IBOutlet UILabel *tipLable;
@property (strong, nonatomic) IBOutlet UIButton *tipButton; // 重设/(取消)的提示按钮

@property (nonatomic, strong) NSString* savedPassword; // 本地存储的密码
@property (nonatomic, strong) NSString* passwordOld; // 旧密码
@property (nonatomic, strong) NSString* passwordNew; // 新密码
@property (nonatomic, strong) NSString* passwordconfirm; // 确认密码
@property (nonatomic, strong) NSString* tip1; // 三步提示语
@property (nonatomic, strong) NSString* tip2;
@property (nonatomic, strong) NSString* tip3;

@end


@implementation LLLockViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {

        // Custom initialization
    
}
    return self;

}

- (id)initWithType:(LLLockViewType)type
{

    self = [super init];
    if (self) {

        _nLockViewType = type;
    
}
    return self;

}

#pragma mark - life cycle
- (void)viewDidLoad
{

    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.indecator.backgroundColor = [UIColor clearColor];
    self.lockview.backgroundColor = [UIColor clearColor];
    
//    self.horiScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 100, 320, 320)];
    
    self.lockview.delegate = self;
    
    LLLog(@"实例化了一个LockVC");

}

- (void)viewWillAppear:(BOOL)animated
{

#ifdef LLLockAnimationOn
    [self capturePreSnap];
#endif
    
    [super viewWillAppear:animated];
    
    // 初始化内容
    switch (_nLockViewType) {

        case LLLockViewTypeCheck:
        {

            _tipLable.text = @"请输入解锁密码";
        
}
            break;
        case LLLockViewTypeCreate:
        {

            _tipLable.text = @"创建手势密码";
        
}
            break;
        case LLLockViewTypeModify:
        {

            _tipLable.text = @"请输入原来的密码";
        
}
            break;
        case LLLockViewTypeClean:
        default:
        {

            _tipLable.text = @"请输入密码以清除密码";
        
}
    
}
    
    // 尝试机会
    nRetryTimesRemain = LLLockRetryTimes;
    
    self.passwordOld = @"";
    self.passwordNew = @"";
    self.passwordconfirm = @"";
    
    // 本地保存的手势密码
    self.savedPassword = [LLLockPassword loadLockPassword];
    LLLog(@"本地保存的密码是%@", self.savedPassword);
    
    [self updateTipButtonStatus];

}

- (void)didReceiveMemoryWarning
{

    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.

}

#pragma mark - 检查/更新密码
- (void)checkPassword:(NSString*)string
{

    // 验证密码正确
    if ([string isEqualToString:self.savedPassword]) {

        
        if (_nLockViewType == LLLockViewTypeModify) {
 // 验证旧密码
            
            self.passwordOld = string; // 设置旧密码,说明是在修改
            
            [self setTip:@"请输入新的密码"]; // 这里和下面的delegate不一致,有空重构
            
        
} else if (_nLockViewType == LLLockViewTypeClean) {
 // 清除密码

            [LLLockPassword saveLockPassword:nil];
            [self hide];
            
            [self showAlert:self.tip2];
            
        
} else {
 // 验证成功
            
            [self hide];
        
}
        
    
}
    // 验证密码错误
    else if (string.length > 0) {

        
        nRetryTimesRemain--;
        
        if (nRetryTimesRemain > 0) {

            
            if (1 == nRetryTimesRemain) {

                [self setErrorTip:[NSString stringWithFormat:@"最后的机会咯-_-!"]
                           errorPswd:string];
            
} else {

                [self setErrorTip:[NSString stringWithFormat:@"密码错误,还可以再输入%d次", nRetryTimesRemain]
                           errorPswd:string];
            
}
            
        
} else {

            
            // 强制注销该账户,并清除手势密码,以便重设
            [self dismissViewControllerAnimated:NO completion:nil]; // 由于是强制登录,这里必须以NO ani的方式才可
            [LLLockPassword saveLockPassword:nil];
            
            [self showAlert:@"超过最大次数,这里该做一些如强制退出重设密码等操作"];
        
}
        
    
} else {

        NSAssert(YES, @"意外情况");
    
}

}

- (void)createPassword:(NSString*)string
{

    // 输入密码
    if ([self.passwordNew isEqualToString:@""] && [self.passwordconfirm isEqualToString:@""]) {

        
        self.passwordNew = string;
        [self setTip:self.tip2];
    
}
    // 确认输入密码
    else if (![self.passwordNew isEqualToString:@""] && [self.passwordconfirm isEqualToString:@""]) {


        self.passwordconfirm = string;
        
        if ([self.passwordNew isEqualToString:self.passwordconfirm]) {

            // 成功
            LLLog(@"两次密码一致");
            
            [LLLockPassword saveLockPassword:string];
            
            [self showAlert:self.tip3];
            
            [self hide];
            
        
} else {

            
            self.passwordconfirm = @"";
            [self setTip:self.tip2];
            [self setErrorTip:@"与上一次绘制不一致,请重新绘制" errorPswd:string];
            
        
}
    
} else {

        NSAssert(1, @"设置密码意外");
    
}

}

#pragma mark - 显示提示
- (void)setTip:(NSString*)tip
{

    [_tipLable setText:tip];
    [_tipLable setTextColor:kTipColorNormal];
    
    _tipLable.alpha = 0;
    [UIView animateWithDuration:0.8
                     animations:^{

                          _tipLable.alpha = 1;
                     
}completion:^(BOOL finished){

                     
}
     ];

}

// 错误
- (void)setErrorTip:(NSString*)tip errorPswd:(NSString*)string
{

    // 显示错误点点
    [self.lockview showErrorCircles:string];
    
    // 直接_变量的坏处是
    [_tipLable setText:tip];
    [_tipLable setTextColor:kTipColorError];
    
    [self shakeAnimationForView:_tipLable];

}

#pragma mark 新建/修改后保存
- (void)updateTipButtonStatus
{

    LLLog(@"重设TipButton");
    if ((_nLockViewType == LLLockViewTypeCreate || _nLockViewType == LLLockViewTypeModify) &&
        ![self.passwordNew isEqualToString:@""]) // 新建或修改 & 确认时 才显示按钮
    {

        [self.tipButton setTitle:@"点击此处以重新开始" forState:UIControlStateNormal];
        [self.tipButton setAlpha:1.0];
        
    
} else {

        [self.tipButton setAlpha:0.0];
    
}
    
    // 更新指示圆点
    if (![self.passwordNew isEqualToString:@""] && [self.passwordconfirm isEqualToString:@""]){

        self.indecator.hidden = NO;
        [self.indecator setPasswordString:self.passwordNew];
    
} else {

        self.indecator.hidden = YES;
    
}

}

#pragma mark - 点击了按钮
- (IBAction)tipButtonPressed:(id)sender {

    self.passwordNew = @"";
    self.passwordconfirm = @"";
    [self setTip:self.tip1];
    [self updateTipButtonStatus];

}

#pragma mark - 成功后返回
- (void)hide
{

    switch (_nLockViewType) {

            
        case LLLockViewTypeCheck:
        {

        
}
            break;
        case LLLockViewTypeCreate:
        case LLLockViewTypeModify:
        {

            [LLLockPassword saveLockPassword:self.passwordNew];
        
}
            break;
        case LLLockViewTypeClean:
        default:
        {

            [LLLockPassword saveLockPassword:nil];
        
}
    
}
    
    // 在这里可能需要回调上个页面做一些刷新什么的动作

#ifdef LLLockAnimationOn
     [self captureCurrentSnap];
    // 隐藏控件
    for (UIView* v in self.view.subviews) {

        if (v.tag > 10000) continue;
        v.hidden = YES;
    
}
    // 动画解锁
    [self animateUnlock];
#else
    [self dismissViewControllerAnimated:NO completion:nil];
#endif
    

}

#pragma mark - delegate 每次划完手势后
- (void)lockString:(NSString *)string
{

    LLLog(@"这次的密码=--->%@<---", string) ;
    
    switch (_nLockViewType) {

            
        case LLLockViewTypeCheck:
        {

            self.tip1 = @"请输入解锁密码";
            [self checkPassword:string];
        
}
            break;
        case LLLockViewTypeCreate:
        {

            self.tip1 = @"创建解锁密码";
            self.tip2 = @"请再次绘制解锁密码";
            self.tip3 = @"解锁密码创建成功";
            [self createPassword:string];
        
}
            break;
        case LLLockViewTypeModify:
        {

            if ([self.passwordOld isEqualToString:@""]) {

                self.tip1
...
...
(Please download the complete source code to view)
			
...
Expand> <Close

Want complete source code? Download it here

Point(s): 1

Download
0 lines left, continue to read

File list

Tips: You can preview the content of files by clicking file names^_^
Name Size Date
.gitignore495.00 B2015-08-21|08:09
01.97 kB
01.97 kB
entries419.00 B2015-08-21|08:09
01.97 kB
project.pbxproj.svn-base20.23 kB2015-08-21|08:09
project.pbxproj20.42 kB2015-08-21|08:09
01.97 kB
01.97 kB
entries420.00 B2015-08-21|08:09
01.97 kB
contents.xcworkspacedata.svn-base155.00 B2015-08-21|08:09
contents.xcworkspacedata155.00 B2015-08-21|08:09
01.97 kB
AppDelegate.h470.00 B2015-08-21|08:09
AppDelegate.m2.93 kB2015-08-21|08:09
01.97 kB
LaunchScreen.xib3.65 kB2015-08-21|08:09
Main.storyboard10.38 kB2015-08-21|08:09
FirstViewController.h258.00 B2015-08-21|08:09
FirstViewController.m808.00 B2015-08-21|08:09
01.97 kB
01.97 kB
Contents.json585.00 B2015-08-21|08:09
01.97 kB
01.97 kB
Contents.json165.00 B2015-08-21|08:09
circle_indecator.pdf17.13 kB2015-08-21|08:09
01.97 kB
Contents.json162.00 B2015-08-21|08:09
circle_normal.pdf44.45 kB2015-08-21|08:09
01.97 kB
Contents.json164.00 B2015-08-21|08:09
circle_selected.pdf45.65 kB2015-08-21|08:09
01.97 kB
Contents.json161.00 B2015-08-21|08:09
circle_wrong.pdf22.26 kB2015-08-21|08:09
01.97 kB
Contents.json3.02 kB2015-08-21|08:09
01.97 kB
Contents.json154.00 B2015-08-21|08:09
first.pdf2.41 kB2015-08-21|08:09
01.97 kB
Contents.json155.00 B2015-08-21|08:09
second.pdf2.37 kB2015-08-21|08:09
Info.plist1.39 kB2015-08-21|08:09
01.97 kB
LLLockConfig.h297.00 B2015-08-21|08:09
LLLockIndicator.h301.00 B2015-08-21|08:09
LLLockIndicator.m2.54 kB2015-08-21|08:09
LLLockPassword.h433.00 B2015-08-21|08:09
LLLockPassword.m1.27 kB2015-08-21|08:09
LLLockView.h530.00 B2015-08-21|08:09
LLLockView.m8.56 kB2015-08-21|08:09
LLLockViewController.h884.00 B2015-08-21|08:09
LLLockViewController.m13.94 kB2015-08-21|08:09
LLLockViewController.xib7.58 kB2015-08-21|08:09
SecondViewController.h300.00 B2015-08-21|08:09
SecondViewController.m2.25 kB2015-08-21|08:09
main.m335.00 B2015-08-21|08:09
01.97 kB
Info.plist748.00 B2015-08-21|08:09
LockSampleTests.m869.00 B2015-08-21|08:09
...

LLLockViewController.m (87.24 kB)

Need 1 point
Your Point(s)

Your Point isn't enough.

Get point immediately by PayPal

More(Debit card / Credit card / PayPal Credit / Online Banking)

Submit your source codes. Get more point

LOGIN

Don't have an account? Register now
Need any help?
Mail to: support@codeforge.com

切换到中文版?

CodeForge Chinese Version
CodeForge English Version

Where are you going?

^_^"Oops ...

Sorry!This guy is mysterious, its blog hasn't been opened, try another, please!
OK

Warm tip!

CodeForge to FavoriteFavorite by Ctrl+D