CalculatorViewController.m 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. #import "CalculatorViewController.h"
  2. #import <Masonry.h>
  3. #import "connectDeviceManager.h"
  4. #import "RSATool.h"
  5. @interface CalculatorViewController ()
  6. {
  7. UIView *bgView;/*引导视图*/
  8. }
  9. @end
  10. @implementation CalculatorViewController
  11. @synthesize pwd;
  12. #define BUTTON
  13. - (void)viewDidLoad {
  14. [super viewDidLoad];
  15. // Do any additional setup after loading the view, typically from a nib.
  16. self.view.backgroundColor=[UIColor blackColor];
  17. CGFloat mWidth=self.view.frame.size.width;
  18. CGFloat mHieght=self.view.frame.size.height;
  19. //加载button
  20. [self loadButtons:mWidth Hight:mHieght];
  21. //加载textField
  22. [self loadTextField:mWidth Hight:mHieght];
  23. //初始化Calculator
  24. _cl=[[Calculator alloc]init];
  25. /*加载引导视图*/
  26. BOOL haveOpenMask = [HWDataManager getBoolWithKey:Const_Mask_View_Open_Calcuator_Tips];
  27. if (haveOpenMask == NO){
  28. [self addGuideView];
  29. }
  30. }
  31. - (void)addGuideView{
  32. bgView = [[UIView alloc] init];
  33. [bgView setBackgroundColor:HW000000Color60];
  34. [self.view addSubview:bgView];
  35. [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
  36. make.left.mas_equalTo(0);
  37. make.bottom.mas_equalTo(0);
  38. make.right.mas_equalTo(0);
  39. make.top.mas_equalTo(0);
  40. }];
  41. /*箭头视图*/
  42. UIImageView *flagImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"guide_open_calcu_flag1"]];
  43. [bgView addSubview:flagImageView];
  44. [flagImageView mas_makeConstraints:^(MASConstraintMaker *make) {
  45. make.right.mas_equalTo(-118.f);
  46. make.height.mas_equalTo(67);
  47. make.width.mas_equalTo(22);
  48. make.top.equalTo(_TF.mas_centerY).offset(40);
  49. }];
  50. /*不可点击btn*/
  51. UIButton *unableBtn = [[UIButton alloc] init];
  52. unableBtn.frame = CGRectMake(0, 0, 295, 64);
  53. NSString * curStr = NSLocalizedString(@"guide_set_open_calcula_guide_tips",nil);
  54. CGFloat curHeight = [curStr boundingRectWithSize:CGSizeMake(295 - 20 , 1000) options:(NSStringDrawingUsesLineFragmentOrigin) attributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:14.f]} context:nil].size.height;
  55. if(curHeight < 50){
  56. curHeight = 50;
  57. }
  58. else{
  59. curHeight += 20;
  60. }
  61. // gradient
  62. CAGradientLayer *gl = [CAGradientLayer layer];
  63. gl.frame = CGRectMake(0,0,295,curHeight);
  64. gl.startPoint = CGPointMake(0, 0.5);
  65. gl.endPoint = CGPointMake(1, 0.5);
  66. gl.colors = @[(__bridge id)HW0CDEFDColor.CGColor, (__bridge id)HW058DFBColor.CGColor];
  67. gl.locations = @[@(0), @(1.0f)];
  68. [unableBtn.layer addSublayer:gl];
  69. [unableBtn setUserInteractionEnabled:NO];
  70. [unableBtn setTitle:curStr forState:(UIControlStateNormal)];
  71. [unableBtn setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
  72. [unableBtn.titleLabel setFont:[UIFont systemFontOfSize:14.f]];
  73. [unableBtn.titleLabel setNumberOfLines:0];
  74. [unableBtn.layer setCornerRadius:curHeight/2.0];
  75. unableBtn.clipsToBounds = YES;
  76. [bgView addSubview:unableBtn];
  77. [unableBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  78. make.right.mas_equalTo(-20*AUTOSCALE);
  79. make.width.mas_equalTo(295);
  80. make.top.equalTo(flagImageView.mas_bottom).offset(-8);
  81. make.height.mas_equalTo(curHeight);
  82. }];
  83. unableBtn.contentEdgeInsets = UIEdgeInsetsMake(0,10, 0, 10);
  84. UIButton *knowBtn = [[UIButton alloc] init];
  85. [knowBtn setBackgroundColor:[UIColor clearColor]];
  86. [knowBtn.layer setCornerRadius:18.f];
  87. [knowBtn.layer setBorderColor:HWFFFFFFColor.CGColor];
  88. [knowBtn.layer setBorderWidth:1];
  89. [knowBtn setTitle:NSLocalizedString(@"guide_set_pwd_guide_know",nil) forState:(UIControlStateNormal)];
  90. [knowBtn setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
  91. [knowBtn.titleLabel setFont:[UIFont systemFontOfSize:16]];
  92. [bgView addSubview:knowBtn];
  93. [knowBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  94. make.right.mas_equalTo(-29*AUTOSCALE);
  95. make.width.mas_equalTo(120);
  96. make.top.equalTo(unableBtn.mas_bottom).offset(15);
  97. make.height.mas_equalTo(36.f);
  98. }];
  99. [knowBtn addTarget:self
  100. action:@selector(knowBtnPressed)
  101. forControlEvents:(UIControlEventTouchUpInside)];
  102. }
  103. - (void)knowBtnPressed{
  104. [bgView removeFromSuperview];
  105. [HWDataManager setBoolWithKey:Const_Mask_View_Open_Calcuator_Tips value:YES];
  106. }
  107. #pragma mark ----样式设置----
  108. -(void)loadButtons:(CGFloat)mWidth Hight:(CGFloat)mHieght{
  109. CGFloat x=1;
  110. CGFloat y=mHieght-(((mWidth-5)/4)*5)-1 - 60;
  111. if(mHieght == 736){//修改 8P手机UI问题
  112. y += 40;
  113. }
  114. NSArray *buttonArray=@[@"C",@"+/-",@"%",@"÷",@"7",@"8",@"9",@"×",@"4",@"5",@"6",@"—",@"1",@"2",@"3",@"+",@"0",@".",@"="];
  115. NSInteger buttonArrayID=0;
  116. for (int i=0; i<5; i++) {
  117. for (int j=0; j<(i==4?3:4); j++) {
  118. UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];
  119. button.frame=CGRectMake(x, y, (buttonArrayID==16?(mWidth-5)/4*2+1 - 13:(mWidth-5)/4 - 13), (mWidth-5)/4 - 13);
  120. button.layer.cornerRadius = ((mWidth-5)/4 - 13)/2;
  121. [button setTitle:[buttonArray objectAtIndex:buttonArrayID] forState:UIControlStateNormal];
  122. button.titleLabel.font=[UIFont systemFontOfSize:40];
  123. button.tag=100+buttonArrayID;
  124. [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  125. if (buttonArrayID%4==3||buttonArrayID==[buttonArray count]-1) {
  126. button.backgroundColor=HWEFA23BColor;
  127. }else if(buttonArrayID==16){
  128. //设置 0 Button 文字向右对齐
  129. [button setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, 0, (mWidth-5)/4)];
  130. x+=((mWidth-5)/4)+1;
  131. button.backgroundColor=HW333333Color9;
  132. }else if(buttonArrayID<3){
  133. button.backgroundColor=HWA5A5A5Color;
  134. [button setTitleColor: [UIColor blackColor] forState:UIControlStateNormal] ;
  135. }else{
  136. button.backgroundColor=HW333333Color9;
  137. }
  138. [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
  139. [self.view addSubview:button];
  140. x+=((mWidth-5)/4)+1;
  141. buttonArrayID++;
  142. }
  143. x=1;
  144. y+=((mWidth-5)/4)+1;
  145. }
  146. }
  147. -(void)loadTextField:(CGFloat)mWidth Hight:(CGFloat)mHieght{
  148. _TF=[[UITextField alloc]initWithFrame:CGRectMake(10, 50, mWidth-20, mHieght-(((mWidth-5)/4)*5)-50)];
  149. _TF.tag=1000;
  150. _TF.textColor=[UIColor whiteColor];
  151. [_TF setEnabled:NO];
  152. _TF.textAlignment=NSTextAlignmentRight;
  153. _TF.adjustsFontSizeToFitWidth=YES;//自适应宽度
  154. _TF.text=@"0";
  155. _TF.font=[UIFont fontWithName:@"HelveticaNeue-Thin" size:80];
  156. [self.view addSubview:_TF];
  157. }
  158. #pragma mark ----button响应----
  159. //button响应方法
  160. -(void)buttonAction:(UIButton *)sender{
  161. [self removeTextFieldFormat];
  162. switch (sender.tag) {
  163. case 100:
  164. //清除
  165. if ([sender.titleLabel.text isEqualToString:@"AC"]) {
  166. [self buttonSymbolStyleReduction:sender];
  167. }
  168. self.TF.text=@"0";
  169. [sender setTitle:@"AC" forState:UIControlStateNormal];
  170. [self.cl calculatorEmpty];
  171. break;
  172. case 101:
  173. //正负转换
  174. [self getPlusMinus:self.TF.text];
  175. break;
  176. case 102:
  177. //百分比
  178. [self getPercentage:self.TF.text];
  179. break;
  180. case 103:
  181. //除
  182. [self execute];
  183. self.cl.exType=1;
  184. [self buttonSymbolStyleReduction:sender];
  185. [self buttonSymbolStyle:sender];
  186. break;
  187. case 107:
  188. //乘
  189. [self execute];
  190. self.cl.exType=2;
  191. [self buttonSymbolStyleReduction:sender];
  192. [self buttonSymbolStyle:sender];
  193. break;
  194. case 111:
  195. //减
  196. [self execute];
  197. self.cl.exType=3;
  198. [self buttonSymbolStyleReduction:sender];
  199. [self buttonSymbolStyle:sender];
  200. break;
  201. case 115:
  202. //加
  203. [self execute];
  204. self.cl.exType=4;
  205. [self buttonSymbolStyleReduction:sender];
  206. [self buttonSymbolStyle:sender];
  207. break;
  208. case 117:
  209. //小数点
  210. [self decimalPoint:self.TF Button:sender];
  211. break;
  212. case 118:
  213. //等
  214. [self execute];
  215. self.cl.exType=0;
  216. [self buttonSymbolStyleReduction:sender];
  217. // self.TF.text=@"0";
  218. break;
  219. default:
  220. //判断清空计数
  221. if (self.emptyCount!=0) {
  222. self.TF.text=@"";
  223. self.emptyCount=0;
  224. }
  225. [self buttonSymbolStyleReduction:sender];
  226. [(UIButton *)[self.view viewWithTag:100] setTitle:@"C" forState:UIControlStateNormal];
  227. [self textFieldValueChange:self.TF Button:sender];
  228. break;
  229. }
  230. [self addTextFieldFormat];
  231. }
  232. #pragma mark ----样式处理----
  233. //添加点击运算符号button样式方法
  234. -(void)buttonSymbolStyle:(UIButton *)sender{
  235. [sender.layer setBorderWidth:2];
  236. }
  237. //还原运算符号button样式方法
  238. -(void)buttonSymbolStyleReduction:(UIButton *)sender{
  239. [((UIButton *)[self.view viewWithTag:103]).layer setBorderWidth:0] ;
  240. [((UIButton *)[self.view viewWithTag:107]).layer setBorderWidth:0] ;
  241. [((UIButton *)[self.view viewWithTag:111]).layer setBorderWidth:0] ;
  242. [((UIButton *)[self.view viewWithTag:115]).layer setBorderWidth:0] ;
  243. [sender.layer setBorderWidth:0];
  244. }
  245. //添加textfield格式
  246. -(void)addTextFieldFormat{
  247. NSMutableString *mutableStr=[NSMutableString stringWithString:self.TF.text];
  248. NSInteger mutableStrLength=mutableStr.length;
  249. //判断是否包含小数点 如果包含获取小数点左边的数字个数mutableStrLength
  250. if ([mutableStr containsString:@"."]) {
  251. mutableStrLength=[self getPointLeft:mutableStr];
  252. }else{
  253. mutableStrLength=[self getNumberCount:mutableStr];
  254. }
  255. for(int i=3 ; i<mutableStrLength ; i++ ){
  256. //每三个数字添加一次分隔符
  257. if (i%3==0) {
  258. //添加分隔逗号
  259. if (mutableStrLength>i) {
  260. NSInteger index=mutableStr.length-i-(mutableStr.length-mutableStrLength);
  261. NSRange range=NSMakeRange(index-1, 1);
  262. //如果上一个不是负号则添加分隔逗号
  263. if (![[mutableStr substringWithRange:range]isEqualToString:@"-"]) {
  264. //如果第一个是负号 分隔符位置+1
  265. if ([[mutableStr substringWithRange:NSMakeRange(0, 1)]isEqualToString:@"-"]) {
  266. [mutableStr insertString:@"," atIndex:index+1];
  267. }else{
  268. [mutableStr insertString:@"," atIndex:index];
  269. }
  270. }else{
  271. [mutableStr insertString:@"," atIndex:index+1];
  272. }
  273. }
  274. }
  275. }
  276. //如果textfield为空时 自动补0
  277. if ([mutableStr isEqualToString:@""]) {
  278. mutableStr=[NSMutableString stringWithString:@"0"];
  279. }
  280. self.TF.text=mutableStr;
  281. }
  282. //去除textfield格式
  283. -(void)removeTextFieldFormat{
  284. if ([self.TF.text isEqualToString:@"0"]) {
  285. self.TF.text=@"";
  286. }
  287. self.TF.text=[self.TF.text stringByReplacingOccurrencesOfString:@"," withString:@""];
  288. }
  289. //textField内容处理方法 限制输入长度 添加分隔符
  290. -(void)textFieldValueChange:(UITextField *)tf Button:(UIButton *)sender{
  291. NSString *buttonTitleStr=sender.titleLabel.text;
  292. if ([self getNumberCount:tf.text]<9) {
  293. tf.text=[tf.text stringByAppendingString:buttonTitleStr];
  294. }
  295. NSLog(@"tf.text = %@",tf.text);
  296. if(tf.text.length == 4){
  297. [self verifyPwdFun:tf.text];
  298. }
  299. // if ([tf.text isEqualToString:pwd]){
  300. // [connectDeviceManager shareInstance].isFirstInputPwdDone = YES;
  301. //
  302. // [[NSNotificationCenter defaultCenter] postNotificationName:ShowImgAndVoiceNotification object:nil];
  303. //
  304. // [self.view removeFromSuperview];
  305. // }
  306. }
  307. #pragma mark 验证密码是否正确
  308. -(void)verifyPwdFun:(NSString*)text{
  309. /*先判断本地有无设备 无设备时需要先扫码添加设备*/
  310. NSDictionary *deviceDict = [HWDataManager getObjectWithKey:Const_Have_Add_Device];
  311. if (deviceDict && [[deviceDict allKeys] containsObject:Const_Have_Add_Device_SN]){
  312. //有设备了先去做链接准备 // 80bec9c5
  313. NSString *SNStr = deviceDict[@"Const_Have_Add_Device_SN"];
  314. if(SNStr){
  315. [[connectDeviceManager shareInstance] getThridMsgBySN:SNStr needReconnect:NO didNetEnd:^(bool didSuc) {
  316. if(didSuc){
  317. NSString *curPwd = [connectDeviceManager shareInstance].DeviceThirdIdMod.data.password;
  318. NSString*desPwdStr = [RSATool AES128Decrypt:curPwd key:AESCODEKEEYY];
  319. if ([desPwdStr isEqualToString:text]){
  320. [connectDeviceManager shareInstance].isFirstInputPwdDone = YES;
  321. [self.view removeFromSuperview];
  322. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  323. [[NSNotificationCenter defaultCenter] postNotificationName:ShowImgAndVoiceNotification object:nil];
  324. });
  325. }
  326. }
  327. else{
  328. }
  329. }];
  330. }
  331. }
  332. }
  333. //小数点限制方法
  334. -(void)decimalPoint:(UITextField *)tf Button:(UIButton *)sender{
  335. //判断是否已经输入小数点
  336. if (![tf.text containsString:@"."]) {
  337. tf.text=[tf.text stringByAppendingString:sender.titleLabel.text];
  338. }
  339. //判断是否直接输入小数点
  340. if([self.TF.text isEqualToString:@"."]){
  341. self.TF.text=@"0.";
  342. }
  343. }
  344. //获取小数点左边数字个数
  345. -(NSInteger)getPointLeft:(NSString *)str{
  346. NSInteger pointLeft=str.length;
  347. //判断是否包含小数点 如果包含获取小数点左边的数字个数
  348. if ([str containsString:@"."]) {
  349. NSRange range = [str rangeOfString:@"."];
  350. NSLog(@"getPointLeft=%ld",range.location);
  351. pointLeft=range.location;
  352. }
  353. //判断是否包含负号
  354. if ([str containsString:@"-"]) {
  355. pointLeft-=1;
  356. }
  357. return pointLeft;
  358. }
  359. //获取小数点右边数字个数
  360. -(NSInteger)getPointRight:(NSString *)str{
  361. NSInteger pointRight=0;
  362. //判断是否包含小数点 如果包含获取小数点右边的数字个数
  363. if ([str containsString:@"."]) {
  364. NSRange range = [str rangeOfString:@"."];
  365. NSLog(@"getPointRight=%ld",str.length- range.location);
  366. pointRight=str.length- range.location;
  367. }
  368. return pointRight;
  369. }
  370. //获取纯数字个数
  371. -(NSInteger)getNumberCount:(NSString *)str{
  372. NSInteger numberCount=str.length;
  373. //判断是否包含小数点
  374. if ([str containsString:@"."]) {
  375. numberCount-=1;
  376. }
  377. //判断是否包含负号
  378. if ([str containsString:@"-"]) {
  379. numberCount-=1;
  380. }
  381. //判断是否包含分隔逗号
  382. if ([str containsString:@","]) {
  383. if ([self getPointLeft:str]<5) {
  384. numberCount-=1;
  385. }else{
  386. numberCount-=2;
  387. }
  388. }
  389. return numberCount;
  390. }
  391. //UIColor 转UIImage
  392. - (UIImage*) createImageWithColor: (UIColor*) color{
  393. CGRect rect=CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
  394. UIGraphicsBeginImageContext(rect.size);
  395. CGContextRef context = UIGraphicsGetCurrentContext();
  396. CGContextSetFillColorWithColor(context, [color CGColor]);
  397. CGContextFillRect(context, rect);
  398. UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
  399. UIGraphicsEndImageContext();
  400. return theImage;
  401. }
  402. - (void)didReceiveMemoryWarning {
  403. [super didReceiveMemoryWarning];
  404. // Dispose of any resources that can be recreated.
  405. }
  406. #pragma mark ----运算----
  407. //正负数转换
  408. -(void)getPlusMinus:(NSString *)number{
  409. self.TF.text=[self.cl getPlusMinus:self.TF.text];
  410. [self textFieldLastValue];
  411. }
  412. //ios计算器bug
  413. //2 * 9 ÷ 目前等于18 转换正负数会变为-0
  414. //计算百分比
  415. -(void)getPercentage:(NSString *)number{
  416. number=[NSString stringWithFormat:@"%f",[number doubleValue]/100];
  417. //去除floa转换后后面多余的0
  418. self.TF.text = [NSString stringFloatHandle:number];
  419. [self textFieldLastValue];
  420. }
  421. //ios计算器bug
  422. //5 * 点击% 变成0.05 再点6 等于30 而不是0.3
  423. //判断textfield上的内容是否为上一次的运算结果 并进行赋值操作 以保证连续运算时执行以上两种操作后的结果能够正确参与运算
  424. -(void)textFieldLastValue{
  425. //如果number1和result都不为空 number2为空 并且清空计数为1(说明刚刚点击过运算符号button) 说明当前textfield上的内容为上一次的运算结果
  426. if (self.cl.number1 != nil) {
  427. if (self.cl.result != nil) {
  428. if (self.cl.number2==nil) {
  429. if (self.emptyCount==1) {
  430. self.cl.number1=self.TF.text;//将转换后的值赋给number1,以便下次运算使用
  431. }
  432. }
  433. }
  434. }
  435. }
  436. //四则运算
  437. -(void)execute{
  438. if(self.emptyCount==0){
  439. if(self.cl.number1 ==nil){
  440. self.cl.number1=self.TF.text;
  441. }else{
  442. self.cl.number2=self.TF.text;
  443. }
  444. //如果number1和number2都不为空 进行计算
  445. if (self.cl.number2 != nil && self.cl.number1 != nil) {
  446. //判断除数是否为0
  447. if([self.cl.number2 isEqualToString:@""]&&self.cl.exType==1){
  448. self.TF.text=@"错误";
  449. }else{
  450. self.TF.text=[self.cl Execute];
  451. }
  452. }
  453. //清空计数
  454. self.emptyCount=1;
  455. }
  456. }
  457. @end