NumLockViewController.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. //
  2. // NumLockViewController.m
  3. // numLockTest
  4. //
  5. // Created by banbu01 on 15-2-5.
  6. // Copyright (c) 2015年 com.koochat.test0716. All rights reserved.
  7. //
  8. #import "NumLockViewController.h"
  9. #import "NumLockButton.h"
  10. #define __MainScreen_Height [[UIScreen mainScreen] bounds].size.height
  11. @interface NumLockViewController ()
  12. {
  13. NSMutableString * _numlockStr;
  14. NSString * _rightStr;
  15. NSString * _oldStr;
  16. UIView * _dropV;
  17. UILabel * _hintLa;
  18. }
  19. @end
  20. @implementation NumLockViewController
  21. - (void)dealloc
  22. {
  23. _numlockStr = nil;
  24. _rightStr = nil;
  25. _dropV = nil;
  26. }
  27. - (void)viewDidLoad {
  28. [super viewDidLoad];
  29. // Do any additional setup after loading the view.
  30. _numlockStr = [[NSMutableString alloc] initWithCapacity:6];
  31. // _rightStr = [[NSMutableString alloc] initWithString:@"123456"];
  32. // self.view.backgroundColor = [UIColor colorWithRed:0.8 green:0.5 blue:0.2 alpha:1.0];
  33. self.view.backgroundColor = [UIColor whiteColor];
  34. [self initNumLockKeyboard];
  35. [self initSmallDrop];
  36. _hintLa = [[UILabel alloc] initWithFrame:CGRectMake(0, (__MainScreen_Height - 218)/2 - 80, JX_SCREEN_WIDTH, 23)];
  37. _hintLa.textAlignment = NSTextAlignmentCenter;
  38. [self.view addSubview:_hintLa];
  39. UIButton * deleteBu = [UIButton buttonWithType:UIButtonTypeCustom];
  40. [deleteBu setFrame:CGRectMake(JX_SCREEN_WIDTH - 100, __MainScreen_Height - 100, 80, 20)];
  41. [deleteBu setTitle:Localized(@"JX_Delete") forState:UIControlStateNormal];
  42. [deleteBu setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
  43. [deleteBu addTarget:self action:@selector(deleteNumlock) forControlEvents:UIControlEventTouchUpInside];
  44. [self.view addSubview:deleteBu];
  45. UIButton *cancelBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, JX_SCREEN_TOP - 46, 46, 46)];
  46. [cancelBtn setBackgroundImage:[UIImage imageNamed:@"title_back_black_big"] forState:UIControlStateNormal];
  47. [cancelBtn addTarget:self action:@selector(cancelBtnAction) forControlEvents:UIControlEventTouchUpInside];
  48. [self.view addSubview:cancelBtn];
  49. _rightStr = [g_default stringForKey:kDeviceLockPassWord];
  50. if (_rightStr.length > 0) {
  51. _hintLa.text = Localized(@"JX_PassWord");
  52. if (!self.isSet && !self.isClose) {
  53. cancelBtn.hidden = YES;
  54. }
  55. }else {
  56. _hintLa.text = Localized(@"JX_SetPassword");
  57. cancelBtn.hidden = NO;
  58. }
  59. }
  60. - (void)cancelBtnAction {
  61. // [self dismissViewControllerAnimated:YES completion:nil];
  62. [self.view removeFromSuperview];
  63. }
  64. - (void)deleteNumlock
  65. {
  66. if (_numlockStr.length > 0)
  67. {
  68. NSString * numStr = [_numlockStr substringToIndex:_numlockStr.length - 1];
  69. [_numlockStr setString:numStr];
  70. UIImageView * dropImg = (UIImageView *)[self.view viewWithTag:_numlockStr.length + 2000];
  71. CATransition *animation = [CATransition animation];
  72. animation.delegate = self;
  73. animation.duration = 0.4;
  74. animation.timingFunction = UIViewAnimationCurveEaseInOut;
  75. animation.type = kCATransitionFade;
  76. [dropImg setImage:[UIImage imageNamed:@"drop"]];
  77. [[dropImg layer] addAnimation:animation forKey:@"animation"];
  78. dropImg.backgroundColor = [UIColor whiteColor];
  79. }
  80. }
  81. #pragma mark - 初始化数字密码键盘
  82. - (void)initNumLockKeyboard
  83. {
  84. for (int i = 0; i < 10; i++)
  85. {
  86. NumLockButton * numBu;
  87. if (i == 0)
  88. {
  89. numBu = [[NumLockButton alloc] initWithNumber:i letters:@""];
  90. [numBu setFrame:CGRectMake((JX_SCREEN_WIDTH - 64) / 2, (__MainScreen_Height - 218)/2 + 228, 64, 64)];
  91. }
  92. else
  93. {
  94. numBu = [[NumLockButton alloc] initWithNumber:i letters:[self lettersForNum:i]];
  95. CGFloat x = 0;
  96. if ((i - 1) % 3 == 0) {
  97. x = (JX_SCREEN_WIDTH - 272) / 2;
  98. }else {
  99. x = (i-1)%3*104 + ((JX_SCREEN_WIDTH - 272) / 2);
  100. }
  101. [numBu setFrame:CGRectMake(x, (__MainScreen_Height - 218)/2+(i-1)/3*76, 64, 64)];
  102. }
  103. numBu.tag = 1000 + i;
  104. [numBu addTarget:self action:@selector(numButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
  105. [self.view addSubview:numBu];
  106. }
  107. }
  108. - (NSString *)lettersForNum:(NSUInteger)num
  109. {
  110. switch (num)
  111. {
  112. case 1:
  113. {
  114. return @" ";
  115. break;
  116. }
  117. case 2:
  118. {
  119. return @"ABC";
  120. break;
  121. }
  122. case 3:
  123. {
  124. return @"DEF";
  125. break;
  126. }
  127. case 4:
  128. {
  129. return @"GHI";
  130. break;
  131. }
  132. case 5:
  133. {
  134. return @"JKL";
  135. break;
  136. }
  137. case 6:
  138. {
  139. return @"MNO";
  140. break;
  141. }
  142. case 7:
  143. {
  144. return @"PQRS";
  145. break;
  146. }
  147. case 8:
  148. {
  149. return @"TUV";
  150. break;
  151. }
  152. case 9:
  153. {
  154. return @"WXYZ";
  155. break;
  156. }
  157. default:
  158. {
  159. return @"";
  160. }
  161. }
  162. return nil;
  163. }
  164. #pragma mark - 初始化密码小圆点
  165. - (void)initSmallDrop
  166. {
  167. _dropV = [[UIView alloc]initWithFrame:CGRectMake((JX_SCREEN_WIDTH - 160) / 2, (__MainScreen_Height - 218)/2 - 40, 160, 12)];
  168. _dropV.backgroundColor = [UIColor clearColor];
  169. [self.view addSubview:_dropV];
  170. for (int i = 0; i < 6; i ++)
  171. {
  172. UIImageView *dropImg = [[UIImageView alloc] initWithFrame:CGRectMake(i * 32, 0, 12, 12)];
  173. [_dropV addSubview:dropImg];
  174. dropImg.tag = 2000 + i;
  175. [dropImg setImage:[UIImage imageNamed:@"drop"]];
  176. dropImg.backgroundColor = [UIColor whiteColor];
  177. dropImg.layer.cornerRadius = dropImg.frame.size.width / 2;
  178. dropImg.layer.masksToBounds = YES;
  179. dropImg.layer.borderColor = [UIColor lightGrayColor].CGColor;
  180. dropImg.layer.borderWidth = 1.0;
  181. }
  182. }
  183. - (void)numButtonPressed:(UIButton *)sender
  184. {
  185. if (_numlockStr.length < 6)
  186. {
  187. [_numlockStr appendFormat:@"%lu",sender.tag - 1000];
  188. NSLog(@"%@",_numlockStr);
  189. UIImageView * dropImg = (UIImageView *)[self.view viewWithTag:_numlockStr.length + 2000 - 1];
  190. CATransition *animation = [CATransition animation];
  191. animation.delegate = self;
  192. animation.duration = 0.4;
  193. animation.timingFunction = UIViewAnimationCurveEaseInOut;
  194. animation.type = kCATransitionFade;
  195. [dropImg setImage:[UIImage imageNamed:@"drop_selected"]];
  196. [[dropImg layer] addAnimation:animation forKey:@"animation"];
  197. dropImg.backgroundColor = [UIColor lightGrayColor];
  198. if (_numlockStr.length == 6)
  199. {
  200. if ([_numlockStr isEqualToString:_rightStr])
  201. {
  202. if (self.isSet) {
  203. _hintLa.text = Localized(@"JX_ResetPassword");
  204. _rightStr = @"";
  205. [_numlockStr setString:@""];
  206. }else {
  207. if ([self.delegate respondsToSelector:@selector(numLockVCSetSuccess:)]) {
  208. [self.delegate numLockVCSetSuccess:self];
  209. }
  210. g_App.isShowDeviceLock = NO;
  211. // [self dismissViewControllerAnimated:YES completion:nil];
  212. [self.view removeFromSuperview];
  213. }
  214. }
  215. else
  216. {
  217. if (_rightStr.length > 0) {
  218. [self startShake:_dropV];
  219. _hintLa.text = Localized(@"JX_PasswordError");
  220. [_numlockStr setString:@""];
  221. }else {
  222. if (_oldStr.length > 0) {
  223. if ([_numlockStr isEqualToString:_oldStr]) {
  224. [g_server showMsg:Localized(@"JX_SetUpSuccess") delay:.5];
  225. [g_default setObject:_numlockStr forKey:kDeviceLockPassWord];
  226. [g_default synchronize];
  227. if ([self.delegate respondsToSelector:@selector(numLockVCSetSuccess:)]) {
  228. [self.delegate numLockVCSetSuccess:self];
  229. }
  230. // [self dismissViewControllerAnimated:YES completion:nil];
  231. [self.view removeFromSuperview];
  232. }else {
  233. _hintLa.text = Localized(@"JX_NotMatch");
  234. _oldStr = @"";
  235. [_numlockStr setString:@""];
  236. [self startShake:_dropV];
  237. }
  238. }else {
  239. _oldStr = [_numlockStr copy];
  240. _hintLa.text = Localized(@"JX_PleaseEnterAgain");
  241. [_numlockStr setString:@""];
  242. }
  243. }
  244. }
  245. for (int i = 0; i < 6; i ++)
  246. {
  247. UIImageView *dropImg = (UIImageView *)[self.view viewWithTag:2000 + i];
  248. [dropImg setImage:[UIImage imageNamed:@"drop"]];
  249. dropImg.backgroundColor = [UIColor whiteColor];
  250. }
  251. }
  252. }
  253. }
  254. #pragma mark - 拖动晃动
  255. - (void)startShake:(UIView* )imageV
  256. {
  257. // 晃动次数
  258. static int numberOfShakes = 4;
  259. // 晃动幅度(相对于总宽度)
  260. static float vigourOfShake = 0.04f;
  261. // 晃动延续时常(秒)
  262. static float durationOfShake = 0.5f;
  263. CAKeyframeAnimation *shakeAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
  264. // 方法一:绘制路径
  265. CGRect frame = imageV.frame;
  266. // 创建路径
  267. CGMutablePathRef shakePath = CGPathCreateMutable();
  268. // 起始点
  269. CGPathMoveToPoint(shakePath, NULL, CGRectGetMidX(frame), CGRectGetMidY(frame));
  270. for (int index = 0; index < numberOfShakes; index++)
  271. {
  272. // 添加晃动路径,固定路径
  273. CGPathAddLineToPoint(shakePath, NULL, CGRectGetMidX(frame) - 20.0f,CGRectGetMidY(frame));
  274. CGPathAddLineToPoint(shakePath, NULL, CGRectGetMidX(frame) + 20.0f,CGRectGetMidY(frame));
  275. // // 添加晃动路径 幅度由大变小
  276. // CGPathAddLineToPoint(shakePath, NULL, CGRectGetMidX(frame) - frame.size.width * vigourOfShake*(1-(float)index/numberOfShakes),CGRectGetMidY(frame));
  277. // CGPathAddLineToPoint(shakePath, NULL, CGRectGetMidX(frame) + frame.size.width * vigourOfShake*(1-(float)index/numberOfShakes),CGRectGetMidY(frame));
  278. }
  279. // 闭合
  280. CGPathCloseSubpath(shakePath);
  281. shakeAnimation.path = shakePath;
  282. shakeAnimation.duration = durationOfShake;
  283. // 释放
  284. CFRelease(shakePath);
  285. [imageV.layer addAnimation:shakeAnimation forKey:kCATransition];
  286. // CABasicAnimation *shakeAnimation = [CABasicAnimation animationWithKeyPath:@"transform"];
  287. // shakeAnimation.duration = 0.08;
  288. // shakeAnimation.autoreverses = YES;
  289. // shakeAnimation.repeatCount = 5;
  290. // shakeAnimation.fromValue = [NSValue valueWithCGRect:CGRectMake(imageV.frame.origin.x - 50, imageV.frame.origin.y, imageV.frame.size.width, imageV.frame.size.height)];//[NSValue valueWithCATransform3D:CATransform3DRotate(imageV.layer.transform, -0.06, 0, 0, 1)];
  291. // shakeAnimation.toValue = [NSValue valueWithCGRect:CGRectMake(imageV.frame.origin.x - 50, imageV.frame.origin.y, imageV.frame.size.width, imageV.frame.size.height)];//[NSValue valueWithCATransform3D:CATransform3DRotate(imageV.layer.transform, 0.06, 0, 0, 1)];
  292. // [imageV.layer addAnimation:shakeAnimation forKey:@"shakeAnimation"];
  293. }
  294. - (void)didReceiveMemoryWarning {
  295. [super didReceiveMemoryWarning];
  296. // Dispose of any resources that can be recreated.
  297. }
  298. /*
  299. #pragma mark - Navigation
  300. // In a storyboard-based application, you will often want to do a little preparation before navigation
  301. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  302. // Get the new view controller using [segue destinationViewController].
  303. // Pass the selected object to the new view controller.
  304. }
  305. */
  306. @end