JKLockController.m 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. //
  2. // JKLockController.m
  3. // Lock
  4. //
  5. // Created by Jack on 2016/10/13.
  6. // Copyright © 2016年 mini1. All rights reserved.
  7. //
  8. #import "JKLockController.h"
  9. #import "JKLockButton.h"
  10. #import "JKDotView.h"
  11. #import "JKMessageBoxController.h"
  12. #import "AppDelegate.h"
  13. #define Password_Length 4
  14. #define Dot_Interval 60
  15. #define Dot_Width 20
  16. #define View_Tag 10086
  17. @interface JKLockController ()
  18. @property (nonatomic,strong) UILabel *tipLabel;///<提示标签
  19. @property (nonatomic,copy) NSString *password;///<密码
  20. @property (nonatomic,strong) UIButton *forgottenButton;
  21. @property (nonatomic,strong) UIButton *resetButton;
  22. @property (nonatomic,strong) NSMutableArray *dotArray;///<密码视图
  23. @property (nonatomic,copy) NSString *input;///<输入
  24. @property (nonatomic,assign) NSUInteger index;///<输入了多少位
  25. @property (nonatomic,copy,readonly) NSString *passwordKey;///<取密码的钥匙
  26. @end
  27. @implementation JKLockController
  28. - (void)setPasswordKey:(NSString *)passwordKey {
  29. _passwordKey = passwordKey;
  30. }
  31. - (instancetype)init {
  32. if (self = [super init]) {
  33. self.backgroundColor = [UIColor colorWithRed:0.8 green:0.8 blue:0.8 alpha:1];
  34. self.dotStrokColor = [UIColor colorWithRed:0.4737 green:0.3641 blue:0.2456 alpha:0.5];
  35. self.dotFillColor = [UIColor colorWithRed:0.4737 green:0.3641 blue:0.2456 alpha:0.5];
  36. self.lockStrokColor = [UIColor colorWithRed:0.4737 green:0.3641 blue:0.2456 alpha:0.5];
  37. self.lockHighlightColor = [UIColor colorWithRed:0.4737 green:0.3641 blue:0.2456 alpha:0.5];
  38. }
  39. return self;
  40. }
  41. - (void)viewDidLoad {
  42. [super viewDidLoad];
  43. // Do any additional setup after loading the view.
  44. self.view.backgroundColor = self.backgroundColor;
  45. self.index = 0;
  46. self.input = @"";
  47. [self loadPassword];
  48. [self configAppearance];
  49. }
  50. - (void)didReceiveMemoryWarning {
  51. [super didReceiveMemoryWarning];
  52. // Dispose of any resources that can be recreated.
  53. }
  54. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
  55. return YES;
  56. }
  57. - (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
  58. [self configAppearance];
  59. }
  60. - (void)clearView {
  61. for (UIView *view in self.view.subviews) {
  62. [view removeFromSuperview];
  63. [self.dotArray removeAllObjects];
  64. }
  65. }
  66. - (void)configAppearance {
  67. CGFloat width = CGRectGetWidth(self.view.bounds);
  68. CGFloat height = CGRectGetHeight(self.view.bounds);
  69. // [self clearView];
  70. // 20
  71. CGFloat y0 = height * 0.2;
  72. self.tipLabel.center = CGPointMake(width / 2, y0 + CGRectGetHeight(self.tipLabel.bounds) / 2);
  73. [self.view addSubview:self.tipLabel];
  74. // 10
  75. CGFloat y1 = y0 + CGRectGetHeight(self.tipLabel.bounds) + 20; // 间距20,高度20
  76. CGFloat x1 = (width - Dot_Width * Password_Length - Dot_Interval * (Password_Length - 1)) / 2;
  77. for (int i = 0; i < Password_Length; i++) {
  78. JKDotView *dotView = [self.view viewWithTag:View_Tag + 20 + i];
  79. if (dotView) {
  80. dotView.frame = CGRectMake(x1 + (Dot_Width + Dot_Interval) * i, y1, Dot_Width, Dot_Width);
  81. } else {
  82. JKDotView *dot = [[JKDotView alloc] initWithFrame:CGRectMake(x1 + (Dot_Width + Dot_Interval) * i, y1, Dot_Width, Dot_Width) strokColor:self.dotStrokColor fillColor:self.dotFillColor];
  83. dot.tag = View_Tag + 20 + i;
  84. [self.view addSubview:dot];
  85. [self.dotArray addObject:dot];
  86. }
  87. }
  88. // 70
  89. // 3 * 4
  90. CGFloat y2 = y1 + Dot_Width + 0.1 * height; // 间距 80 / 40
  91. CGFloat Button_Width = 0.07 * (width > height ? height : width);
  92. CGFloat interval = 0.05 * (width > height ? height : width);
  93. CGFloat x2 = (width - Button_Width * 3 - interval * 2) / 2;
  94. for (int i = 0; i < 3; i++) { // col
  95. for (int j = 0; j < 4; j++) { // row
  96. int index = 3 * j + i + View_Tag + 50;
  97. UIView *view = [self.view viewWithTag:index];
  98. if (i == 0 && j == 3) { // 取消
  99. if (!view) {
  100. UIButton *cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  101. cancelBtn.frame = CGRectMake(x2 + (Button_Width + interval) * i, y2 + (Button_Width + interval) * j, Button_Width, Button_Width);
  102. [cancelBtn setTitle:@"cancel" forState:UIControlStateNormal];
  103. [cancelBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  104. [cancelBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateHighlighted];
  105. [cancelBtn addTarget:self action:@selector(cancelButtonClick:) forControlEvents:UIControlEventTouchUpInside];
  106. [self.view addSubview:cancelBtn];
  107. cancelBtn.tag = index;
  108. } else {
  109. view.frame = CGRectMake(x2 + (Button_Width + interval) * i, y2 + (Button_Width + interval) * j, Button_Width, Button_Width);
  110. }
  111. } else if (i == 2 && j == 3) { // 删除
  112. if (view) {
  113. view.frame = CGRectMake(x2 + (Button_Width + interval) * i, y2 + (Button_Width + interval) * j, Button_Width, Button_Width);
  114. } else {
  115. UIButton *deleteBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  116. deleteBtn.frame = CGRectMake(x2 + (Button_Width + interval) * i, y2 + (Button_Width + interval) * j, Button_Width, Button_Width);
  117. [deleteBtn setTitle:@"delete" forState:UIControlStateNormal];
  118. [deleteBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  119. [deleteBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateHighlighted];
  120. [deleteBtn addTarget:self action:@selector(deleteButtonClick:) forControlEvents:UIControlEventTouchUpInside];
  121. [self.view addSubview:deleteBtn];
  122. deleteBtn.tag = index;
  123. }
  124. } else { // 数字
  125. JKLockButton *btn = (JKLockButton *)view;
  126. if (btn) {
  127. btn.frame = CGRectMake(x2 + (Button_Width + interval) * i, y2 + (Button_Width + interval) * j, Button_Width, Button_Width);
  128. } else {
  129. btn = [[JKLockButton alloc] initWithFrame:CGRectMake(x2 + (Button_Width + interval) * i, y2 + (Button_Width + interval) * j, Button_Width, Button_Width) strokColor:self.lockStrokColor highlightColor:self.lockHighlightColor];
  130. if (i == 1 && j == 3) {
  131. btn.number = 0;
  132. } else {
  133. btn.number = 1 + i + j * 3;
  134. }
  135. [self.view addSubview:btn];
  136. [btn addTarget:self action:@selector(numberButtonClick:) forControlEvents:UIControlEventTouchUpInside];
  137. btn.tag = index;
  138. }
  139. }
  140. }
  141. }
  142. // forget
  143. self.forgottenButton.frame = CGRectMake(width - 120, height - 40, 120, 40);
  144. [self.view addSubview:self.forgottenButton];
  145. // reset
  146. self.resetButton.frame = CGRectMake(0, height - 40, 120, 40);
  147. [self.view addSubview:self.resetButton];
  148. }
  149. - (void)loadPassword {
  150. NSString *password = [[NSUserDefaults standardUserDefaults] objectForKey:self.passwordKey];
  151. self.password = password;
  152. }
  153. - (void)setPassword:(NSString *)password {
  154. _password = password;
  155. if (password) {
  156. self.tipLabel.text = @"Please Input Your Password";
  157. } else {
  158. self.tipLabel.text = @"Please Set A Password";
  159. }
  160. }
  161. - (UILabel *)tipLabel {
  162. if (!_tipLabel) {
  163. _tipLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 20)];
  164. _tipLabel.font = [UIFont systemFontOfSize:20.0f];
  165. _tipLabel.textAlignment = NSTextAlignmentCenter;
  166. }
  167. return _tipLabel;
  168. }
  169. - (UIButton *)forgottenButton {
  170. if (!_forgottenButton) {
  171. _forgottenButton = [UIButton buttonWithType:UIButtonTypeCustom];
  172. [_forgottenButton setTitle:@"forget password" forState:UIControlStateNormal];
  173. [_forgottenButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateHighlighted];
  174. _forgottenButton.tag = View_Tag + 2;
  175. _forgottenButton.titleLabel.font = [UIFont systemFontOfSize:14.0f];
  176. [_forgottenButton addTarget:self action:@selector(forgottenButtonClick:) forControlEvents:UIControlEventTouchUpInside];
  177. }
  178. return _forgottenButton;
  179. }
  180. - (UIButton *)resetButton {
  181. if (!_resetButton) {
  182. _resetButton = [UIButton buttonWithType:UIButtonTypeCustom];
  183. [_resetButton setTitle:@"reset password" forState:UIControlStateNormal];
  184. [_resetButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateHighlighted];
  185. _resetButton.tag = View_Tag + 2;
  186. _resetButton.titleLabel.font = [UIFont systemFontOfSize:14.0f];
  187. [_resetButton addTarget:self action:@selector(resetButtonClick:) forControlEvents:UIControlEventTouchUpInside];
  188. }
  189. return _resetButton;
  190. }
  191. - (NSMutableArray *)dotArray {
  192. if (!_dotArray) {
  193. _dotArray = [NSMutableArray arrayWithCapacity:Password_Length];
  194. }
  195. return _dotArray;
  196. }
  197. - (void)numberButtonClick:(JKLockButton *)sender {
  198. if (self.index < Password_Length) {
  199. NSInteger number = sender.number;
  200. JKDotView *dot = [self.dotArray objectAtIndex:self.index];
  201. dot.state = JKDotStateSelected;
  202. self.input = [self.input stringByAppendingString:[NSString stringWithFormat:@"%ld",(long)number]];
  203. self.index++;
  204. }
  205. if (self.index == Password_Length) {
  206. [self performSelector:@selector(verifyPassword) withObject:nil afterDelay:0.5];
  207. }
  208. }
  209. - (void)cancelButtonClick:(UIButton *)sender {
  210. if (self.navigationController) {
  211. [self.navigationController popViewControllerAnimated:YES];
  212. } else {
  213. [self dismissViewControllerAnimated:YES completion:nil];
  214. }
  215. }
  216. - (void)deleteButtonClick:(UIButton *)sender {
  217. if (self.index > 0) {
  218. JKDotView *dot = [self.dotArray objectAtIndex:self.index - 1];
  219. dot.state = JKDotStateNormal;
  220. self.input = [self.input substringToIndex:self.index - 1];
  221. self.index--;
  222. }
  223. }
  224. - (void)forgottenButtonClick:(UIButton *)sender {
  225. JKMessageBoxController *messageBoxController = [JKMessageBoxController messageBoxControllerWithTip:@"Please enter your login password"];
  226. __weak typeof(messageBoxController) weakVC = messageBoxController;
  227. __weak typeof(self) weakself = self;
  228. messageBoxController.changeHandler = ^(UITextField *textField,NSRange range,NSString *string) {
  229. return YES;
  230. };
  231. messageBoxController.textHandler = ^(NSString *text){
  232. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  233. if ([text isEqualToString:appDelegate.password]) {
  234. // [[NSUserDefaults standardUserDefaults] removeObjectForKey:self.passwordKey];
  235. // weakself.password = nil;
  236. weakself.index = 0;
  237. weakself.input = @"";
  238. for (JKDotView *dot in self.dotArray) {
  239. dot.state = JKDotStateNormal;
  240. }
  241. // weakself.tipLabel.text = @"Please Set A New Password";
  242. [weakVC dismissViewControllerAnimated:YES completion:^{
  243. // weakself.tipLabel.text = @"Please Set A New Password";
  244. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Your Current Password" message:weakself.password preferredStyle:UIAlertControllerStyleAlert];
  245. UIAlertAction *action = [UIAlertAction actionWithTitle:@"ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  246. if (alertController.navigationController) {
  247. [alertController.navigationController popViewControllerAnimated:YES];
  248. } else {
  249. [alertController dismissViewControllerAnimated:YES completion:nil];
  250. }
  251. }];
  252. [alertController addAction:action];
  253. [weakself presentViewController:alertController animated:YES completion:nil];
  254. }];
  255. } else {
  256. [weakVC warning:@"It's wrong,please try again"];
  257. }
  258. };
  259. [self presentViewController:messageBoxController animated:YES completion:nil];
  260. }
  261. - (void)resetButtonClick:(UIButton *)sender {
  262. JKMessageBoxController *messageBoxController = [JKMessageBoxController messageBoxControllerWithTip:@"Please enter your login password"];
  263. __weak typeof(messageBoxController) weakVC = messageBoxController;
  264. __weak typeof(self) weakself = self;
  265. messageBoxController.changeHandler = ^(UITextField *textField,NSRange range,NSString *string) {
  266. return YES;
  267. };
  268. messageBoxController.textHandler = ^(NSString *text){
  269. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  270. if ([text isEqualToString:appDelegate.password]) {
  271. [[NSUserDefaults standardUserDefaults] removeObjectForKey:self.passwordKey];
  272. weakself.password = nil;
  273. weakself.index = 0;
  274. weakself.input = @"";
  275. for (JKDotView *dot in self.dotArray) {
  276. dot.state = JKDotStateNormal;
  277. }
  278. weakself.tipLabel.text = @"Please Set A New Password";
  279. [weakVC dismissViewControllerAnimated:YES completion:^{
  280. weakself.tipLabel.text = @"Please Set A New Password";
  281. }];
  282. } else {
  283. [weakVC warning:@"It's wrong,please try again"];
  284. }
  285. };
  286. [self presentViewController:messageBoxController animated:YES completion:nil];
  287. }
  288. - (void)verifyPassword {
  289. self.index = 0;
  290. for (JKDotView *dot in self.dotArray) {
  291. dot.state = JKDotStateNormal;
  292. }
  293. if (self.password) { // 验证
  294. DebugLog(@"password: %@",self.password);
  295. if ([self.password isEqualToString:self.input]) {
  296. [[NSUserDefaults standardUserDefaults] setObject:self.password forKey:self.passwordKey];
  297. [[NSUserDefaults standardUserDefaults] synchronize];
  298. self.tipLabel.text = @"Please Input Your Password";
  299. [self cancelButtonClick:nil];// 返回前一个页面
  300. sleep(0.25);// 先回到前一个页面才做事情,0.25s为隐式动画持续时间
  301. if (self.authoReturn) {
  302. self.authoReturn(YES);
  303. }
  304. } else {
  305. self.tipLabel.text = @"The Password Doesn't Match,Please Try Again";
  306. }
  307. } else { //设置
  308. self.password = self.input;
  309. self.tipLabel.text = @"Please re-enter password to confirm";
  310. }
  311. self.input = @"";
  312. }
  313. @end