JKLockController.m 16 KB

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