|
|
@@ -0,0 +1,243 @@
|
|
|
+//
|
|
|
+// JKLockController.m
|
|
|
+// Lock
|
|
|
+//
|
|
|
+// Created by Jack on 2016/10/13.
|
|
|
+// Copyright © 2016年 mini1. All rights reserved.
|
|
|
+//
|
|
|
+
|
|
|
+#import "JKLockController.h"
|
|
|
+#import "JKLockButton.h"
|
|
|
+#import "JKDotView.h"
|
|
|
+
|
|
|
+#define Password_Length 4
|
|
|
+#define Dot_Interval 60
|
|
|
+#define Dot_Width 20
|
|
|
+
|
|
|
+
|
|
|
+@interface JKLockController ()
|
|
|
+
|
|
|
+@property (nonatomic,strong) UILabel *tipLabel;///<提示标签
|
|
|
+@property (nonatomic,copy) NSString *password;///<密码
|
|
|
+@property (nonatomic,strong) NSMutableArray *dotArray;///<密码视图
|
|
|
+@property (nonatomic,copy) NSString *input;///<输入
|
|
|
+@property (nonatomic,assign) NSUInteger index;///<输入了多少位
|
|
|
+
|
|
|
+@property (nonatomic,copy,readonly) NSString *passwordKey;///<取密码的钥匙
|
|
|
+
|
|
|
+@end
|
|
|
+
|
|
|
+@implementation JKLockController
|
|
|
+
|
|
|
+- (void)setPasswordKey:(NSString *)passwordKey {
|
|
|
+ _passwordKey = passwordKey;
|
|
|
+}
|
|
|
+
|
|
|
+- (void)viewDidLoad {
|
|
|
+ [super viewDidLoad];
|
|
|
+ // Do any additional setup after loading the view.
|
|
|
+ self.view.backgroundColor = [UIColor colorWithRed:0.5 green:0.8 blue:0.6 alpha:1];
|
|
|
+
|
|
|
+ self.index = 0;
|
|
|
+ self.input = @"";
|
|
|
+ [self loadPassword];
|
|
|
+ [self configAppearance];
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+- (void)didReceiveMemoryWarning {
|
|
|
+ [super didReceiveMemoryWarning];
|
|
|
+ // Dispose of any resources that can be recreated.
|
|
|
+}
|
|
|
+
|
|
|
+- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
|
|
|
+ return YES;
|
|
|
+}
|
|
|
+
|
|
|
+- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
|
|
|
+
|
|
|
+ [self configAppearance];
|
|
|
+}
|
|
|
+
|
|
|
+- (void)clearView {
|
|
|
+ for (UIView *view in self.view.subviews) {
|
|
|
+ [view removeFromSuperview];
|
|
|
+ [self.dotArray removeAllObjects];
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+- (void)configAppearance {
|
|
|
+ CGFloat width = CGRectGetWidth(self.view.bounds);
|
|
|
+ CGFloat height = CGRectGetHeight(self.view.bounds);
|
|
|
+
|
|
|
+ [self clearView];
|
|
|
+
|
|
|
+ // 20
|
|
|
+ CGFloat y0 = height * 0.2;
|
|
|
+ self.tipLabel.center = CGPointMake(width / 2, y0 + CGRectGetHeight(self.tipLabel.bounds) / 2);
|
|
|
+ [self.view addSubview:self.tipLabel];
|
|
|
+
|
|
|
+ // 10
|
|
|
+ CGFloat y1 = y0 + CGRectGetHeight(self.tipLabel.bounds) + 20; // 间距20,高度20
|
|
|
+ CGFloat x1 = (width - Dot_Width * Password_Length - Dot_Interval * (Password_Length - 1)) / 2;
|
|
|
+ for (int i = 0; i < Password_Length; i++) {
|
|
|
+ JKDotView *dot = [[JKDotView alloc] initWithFrame:CGRectMake(x1 + (Dot_Width + Dot_Interval) * i, y1, Dot_Width, Dot_Width)];
|
|
|
+
|
|
|
+ [self.view addSubview:dot];
|
|
|
+ [self.dotArray addObject:dot];
|
|
|
+ }
|
|
|
+
|
|
|
+ // 70
|
|
|
+ // 3 * 4
|
|
|
+ CGFloat y2 = y1 + Dot_Width + 0.1 * height; // 间距 80 / 40
|
|
|
+ CGFloat Button_Width = 0.07 * (width > height ? height : width);
|
|
|
+ CGFloat interval = 0.05 * (width > height ? height : width);
|
|
|
+ CGFloat x2 = (width - Button_Width * 3 - interval * 2) / 2;
|
|
|
+
|
|
|
+ for (int i = 0; i < 3; i++) { // col
|
|
|
+
|
|
|
+ for (int j = 0; j < 4; j++) { // row
|
|
|
+
|
|
|
+
|
|
|
+ if (i == 0 && j == 3) { // 取消
|
|
|
+
|
|
|
+ UIButton *cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
|
|
|
+ cancelBtn.frame = CGRectMake(x2 + (Button_Width + interval) * i, y2 + (Button_Width + interval) * j, Button_Width, Button_Width);
|
|
|
+ [cancelBtn setTitle:@"cancel" forState:UIControlStateNormal];
|
|
|
+ [cancelBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
|
|
|
+ [cancelBtn addTarget:self action:@selector(cancelButtonClick:) forControlEvents:UIControlEventTouchUpInside];
|
|
|
+ [self.view addSubview:cancelBtn];
|
|
|
+
|
|
|
+ } else if (i == 2 && j == 3) { // 删除
|
|
|
+
|
|
|
+ UIButton *deleteBtn = [UIButton buttonWithType:UIButtonTypeCustom];
|
|
|
+ deleteBtn.frame = CGRectMake(x2 + (Button_Width + interval) * i, y2 + (Button_Width + interval) * j, Button_Width, Button_Width);
|
|
|
+ [deleteBtn setTitle:@"delete" forState:UIControlStateNormal];
|
|
|
+ [deleteBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
|
|
|
+ [deleteBtn addTarget:self action:@selector(deleteButtonClick:) forControlEvents:UIControlEventTouchUpInside];
|
|
|
+ [self.view addSubview:deleteBtn];
|
|
|
+
|
|
|
+ } else { // 数字
|
|
|
+ JKLockButton *btn = [[JKLockButton alloc] initWithFrame:CGRectMake(x2 + (Button_Width + interval) * i, y2 + (Button_Width + interval) * j, Button_Width, Button_Width)];
|
|
|
+ if (i == 1 && j == 3) {
|
|
|
+ btn.number = 0;
|
|
|
+ } else {
|
|
|
+ btn.number = 1 + i + j * 3;
|
|
|
+ }
|
|
|
+ [self.view addSubview:btn];
|
|
|
+ [btn addTarget:self action:@selector(numberButtonClick:) forControlEvents:UIControlEventTouchUpInside];
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+- (void)loadPassword {
|
|
|
+
|
|
|
+ NSString *password = [[NSUserDefaults standardUserDefaults] objectForKey:self.passwordKey];
|
|
|
+ self.password = password;
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+- (void)setPassword:(NSString *)password {
|
|
|
+ _password = password;
|
|
|
+ if (password) {
|
|
|
+ self.tipLabel.text = @"Please Input Your Password";
|
|
|
+ } else {
|
|
|
+ self.tipLabel.text = @"Please Set A Password";
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+- (UILabel *)tipLabel {
|
|
|
+ if (!_tipLabel) {
|
|
|
+ _tipLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 20)];
|
|
|
+ _tipLabel.font = [UIFont systemFontOfSize:20.0f];
|
|
|
+ _tipLabel.textAlignment = NSTextAlignmentCenter;
|
|
|
+
|
|
|
+ }
|
|
|
+ return _tipLabel;
|
|
|
+}
|
|
|
+
|
|
|
+- (NSMutableArray *)dotArray {
|
|
|
+ if (!_dotArray) {
|
|
|
+ _dotArray = [NSMutableArray arrayWithCapacity:Password_Length];
|
|
|
+ }
|
|
|
+ return _dotArray;
|
|
|
+}
|
|
|
+
|
|
|
+- (void)numberButtonClick:(JKLockButton *)sender {
|
|
|
+ if (self.index < Password_Length) {
|
|
|
+ NSInteger number = sender.number;
|
|
|
+ JKDotView *dot = [self.dotArray objectAtIndex:self.index];
|
|
|
+ dot.state = JKDotStateSelected;
|
|
|
+
|
|
|
+ self.input = [self.input stringByAppendingString:[NSString stringWithFormat:@"%d",number]];
|
|
|
+ self.index++;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (self.index == Password_Length) {
|
|
|
+
|
|
|
+ [self performSelector:@selector(verifyPassword) withObject:nil afterDelay:0.5];
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+- (void)cancelButtonClick:(UIButton *)sender {
|
|
|
+ if (self.navigationController) {
|
|
|
+ [self.navigationController popViewControllerAnimated:YES];
|
|
|
+ } else {
|
|
|
+ [self dismissViewControllerAnimated:YES completion:nil];
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+- (void)deleteButtonClick:(UIButton *)sender {
|
|
|
+ if (self.index > 0) {
|
|
|
+
|
|
|
+ JKDotView *dot = [self.dotArray objectAtIndex:self.index - 1];
|
|
|
+ dot.state = JKDotStateNormal;
|
|
|
+
|
|
|
+ self.input = [self.input substringToIndex:self.index - 1];
|
|
|
+ self.index--;
|
|
|
+
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+- (void)verifyPassword {
|
|
|
+
|
|
|
+ self.index = 0;
|
|
|
+ for (JKDotView *dot in self.dotArray) {
|
|
|
+ dot.state = JKDotStateNormal;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (self.password) { // 验证
|
|
|
+ NSLog(@"password: %@",self.password);
|
|
|
+ if ([self.password isEqualToString:self.input]) {
|
|
|
+
|
|
|
+ [[NSUserDefaults standardUserDefaults] setObject:self.password forKey:self.passwordKey];
|
|
|
+ [[NSUserDefaults standardUserDefaults] synchronize];
|
|
|
+
|
|
|
+ self.tipLabel.text = @"Please Input Your Password";
|
|
|
+ [self cancelButtonClick:nil];// 返回前一个页面
|
|
|
+ sleep(0.25);// 先回到前一个页面才做事情,0.25s为隐式动画持续时间
|
|
|
+ if (self.authoReturn) {
|
|
|
+ self.authoReturn(YES);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ } else {
|
|
|
+
|
|
|
+ self.tipLabel.text = @"The Password Doesn't Match,Please Try Again";
|
|
|
+ }
|
|
|
+
|
|
|
+ } else { //设置
|
|
|
+
|
|
|
+ self.password = self.input;
|
|
|
+ self.tipLabel.text = @"Please Check The Password Again";
|
|
|
+ }
|
|
|
+ self.input = @"";
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+@end
|