【無限互聯(lián)】IOS開發(fā)之手勢密碼的實(shí)現(xiàn).doc_第1頁
【無限互聯(lián)】IOS開發(fā)之手勢密碼的實(shí)現(xiàn).doc_第2頁
【無限互聯(lián)】IOS開發(fā)之手勢密碼的實(shí)現(xiàn).doc_第3頁
【無限互聯(lián)】IOS開發(fā)之手勢密碼的實(shí)現(xiàn).doc_第4頁
【無限互聯(lián)】IOS開發(fā)之手勢密碼的實(shí)現(xiàn).doc_第5頁
已閱讀5頁,還剩5頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

手勢密碼解析代碼結(jié)構(gòu):GesturePasswordView 用以首頁布局GesturePasswordButton 用以設(shè)置按鈕效果GesturePasswordController 用以實(shí)現(xiàn)各個(gè)功能模塊KeychainItemWrapper 用以保存密碼信息TentacleView 用以設(shè)置輸入密碼時(shí)的線條效果并接受手勢事件代碼分析:密碼首頁布局:GesturePasswordView通過循環(huán)九個(gè)button排列出手勢密碼區(qū),所有的button都存入buttonArray數(shù)組當(dāng)中,并添加了一個(gè) state Label類 用以顯示提示信息objcview plaincopy1. -(id)initWithFrame:(CGRect)frame2. 3. self=superinitWithFrame:frame;4. /首頁密碼視圖布局if(self)5. /Initializationcode6. buttonArray=NSMutableArrayallocinitWithCapacity:0;7. view=UIViewallocinitWithFrame:CGRectMake(frame.size.width/2-160,frame.size.height/2-80,320,320);8. /創(chuàng)建密碼鍵盤小視圖0-9for(inti=0;i9;i+)9. NSIntegerrow=i/3;NSIntegercol=i%3;/ButtonFrame10. /設(shè)置小視圖Frame11. NSIntegerdistance=320/3;12. NSIntegersize=distance/1.5;13. /每個(gè)小視圖間的空隙14. NSIntegermargin=size/4;15. GesturePasswordButton*gesturePasswordButton=GesturePasswordButtonallocinitWith16. Frame:CGRectMake(col*distance+margin,row*distance,size,size);gesturePasswordButtonsetTag:i;17. /把小視圖添加到View視圖18. viewaddSubview:gesturePasswordButton;/將視圖屬性存入數(shù)組19. buttonArrayaddObject:gesturePasswordButton;20. frame.origin.y=0;21. selfaddSubview:view;22. tentacleView=TentacleViewallocinitWithFrame:view.frame;23. /將buttonArray中的View傳給tentacleView中得buttonArraytentacleViewsetButtonArray:buttonArray;24. /實(shí)現(xiàn)代理25. tentacleViewsetTouchBeginDelegate:self;selfaddSubview:tentacleView;26. /布局屏幕第一個(gè)圓形視圖27. imgView=UIImageViewallocinitWithFrame:CGRectMake(frame.size.width/2-35,frame.size.width/2-80,70,70);28. imgViewsetBackgroundColor:UIColorwhiteColor;29. /設(shè)置圓形視圖30. imgView.layersetCornerRadius:35;31. imgView.layersetBorderColor:UIColorgrayColor.CGColor;imgView.layersetBorderWidth:3;32. selfaddSubview:imgView;33. /創(chuàng)建Label用以打印手勢信息34. state=UILabelallocinitWithFrame:CGRectMake(frame.size.width/2-140,frame.size.height/2-120,280,30);35. statesetTextAlignment:NSTextAlignmentCenter;statesetFont:UIFontsystemFontOfSize:14.f;selfaddSubview:state;36. forgetButton=UIButtonallocinitWithFrame:CGRectMake(frame.size.width/2-150,frame.size.height/2+220,120,30);37. forgetButton.titleLabelsetFont:UIFontsystemFontOfSize:14;38. forgetButtonsetTitleColor:UIColorwhiteColorforState:UIControlStateNormal;forgetButtonsetTitle:忘記手勢密碼forState:UIControlStateNormal;39. forgetButtonaddTarget:selfaction:selector(forget)forControlEvents:UIControlEventTouch40. Down;41. selfaddSubview:forgetButton;42. changeButton=UIButtonallocinitWithFrame:CGRectMake(frame.size.width/2+30,frame.size.height/2+220,120,30);43. changeButton.titleLabelsetFont:UIFontsystemFontOfSize:14;44. changeButtonsetTitleColor:UIColorwhiteColorforState:UIControlStateNormal;changeButtonsetTitle:修改手勢密碼forState:UIControlStateNormal;45. changeButtonaddTarget:selfaction:selector(change)forControlEvents:UIControlEventTo46. uchDown;47. selfaddSubview:changeButton;48. 49. returnself;50. 頁沒有使用圖片,面可以通過view己添加并且通過keychain做的數(shù)據(jù)持久化,利用蘋果官方KeychainItemWrapper類keychain存儲(chǔ)的數(shù)據(jù)不會(huì)因?yàn)閯h除app而清除記錄,請調(diào)用-(void)clear清除儲(chǔ)存密碼。代碼如下:objcview plaincopy1. -(BOOL)resetPassword:(NSString*)result2. if(previousStringisEqualToString:)3. previousString=result;4. gesturePasswordView.tentacleViewenterArgin;5. gesturePasswordView.statesetTextColor:UIColorcolorWithRed:2/255.fgreen:174/255.fblue:6. 240/255.falpha:1;7. gesturePasswordView.statesetText:請驗(yàn)證輸入密碼;returnYES;8. /保存密碼信息到本地else9. if(resultisEqualToString:previousString)10. KeychainItemWrapper*keychin=KeychainItemWrapperallocinitWithIdentifier:Gestur11. eaccessGroup:nil;12. keychinsetObject:forKey:(_bridgeid)kSecAttrAccount;13. keychinsetObject:resultforKey:(_bridgeid)kSecValueData;14. /selfpresentViewController:(UIViewController)animated:YEScompletion:nil;gesturePasswordView.statesetTextColor:UIColorcolorWithRed:2/255.fgreen:174/255.fblu15. e:240/255.falpha:1;16. gesturePasswordView.statesetText:已保存手勢密碼;selfdismissViewControllerAnimated:YEScompletion:17. ;18. returnYES;19. else20. previousString=;21. gesturePasswordView.statesetTextColor:UIColorredColor;gesturePasswordView.statesetText:兩次密碼不一致,請重新輸入;returnNO;22. 23. 清除本地密碼信息:objcview plaincopy1. #pragmamark-清空記錄2. -(void)clear3. KeychainItemWrapper*keychin=KeychainItemWrapperallocinitWithIdentifier:GestureaccessGroup:nil;4. keychinresetKeychainItem;主要實(shí)現(xiàn)原理:1. 在GesturePasswordView中導(dǎo)入GesturePasswordButton.h 用通過計(jì)算坐標(biāo)循環(huán)遍歷九個(gè)button排列出手勢密碼區(qū),且添加tag,還添加了一個(gè) state Label類用以顯示提示信息(上圖有述)2. 在GesturePasswordView中通過GesturePasswordButton.h用來設(shè)置各個(gè)button的效果3. 在TentacleView通過- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event接受手勢觸摸事件(代碼如下)objcview plaincopy1. -(void)touchesBegan:(NSSet*)toucheswithEvent:(UIEvent*)event2. CGPointtouchPoint;3. UITouch*touch=touchesanyObject;touchesArrayremoveAllObjects;touchedArrayremoveAllObjects;touchBeginDelegategestureTouchBegin;success=1;4. if(touch)5. touchPoint=touchlocationInView:self;for(inti=0;ibuttonArray.count;i+)6. GesturePasswordButton*buttonTemp=(GesturePasswordButton*)buttonArrayobjectAtIndex:i);7. buttonTempsetSuccess:YES;8. buttonTempsetSelected:NO;9. if(CGRectContainsPoint(buttonTemp.frame,touchPoint)10. CGRectframeTemp=buttonTemp.frame;11. /計(jì)算觸摸點(diǎn)的坐標(biāo)12. CGPointpoint=CGPointMake(frameTemp.origin.x+frameTemp.size.width/2,frameTemp.13. origin.y+frameTemp.size.height/2);14. NSDictionary*dict=NSDictionarydictionaryWithObjectsAndKeys:NSStringstringWithFor15. mat:%f,point.x,x,NSStringstringWithFormat:%f,point.y,y,nilnil;touchesArrayaddObject:dict;16. lineStartPoint=touchPoint;17. buttonTempsetNeedsDisplay;18. selfsetNeedsDisplay;19. 當(dāng)手指在屏幕上移時(shí),就會(huì)調(diào)用- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event方法,計(jì)算出手勢觸摸過的button的信息并保存到數(shù)組中objcview plaincopy1. /手勢移動(dòng)2. -(void)touchesMoved:(NSSet*)toucheswithEvent:(UIEvent*)event3. CGPointtouchPoint;4. UITouch*touch=touchesanyObject;5. if(touch)6. touchPoint=touchlocationInView:self;7. for(inti=0;ibuttonArray.count;i+)8. GesturePasswordButton*buttonTemp=(GesturePasswordButton*)buttonArrayobjectAtIndex:i);9. if(CGRectContainsPoint(buttonTemp.frame,touchPoint)10. if(touchedArraycontainsObject:NSStringstringWithFormat:num%d,i)11. lineEndPoint=touchPoint;12. selfsetNeedsDisplay;13. return;14. 15. touchedArrayaddObject:NSStringstringWithFormat:num%d,i;16. buttonTempsetSelected:YES;17. buttonTempsetNeedsDisplay;18. CGRectframeTemp=buttonTemp.frame;19. CGPointpoint=CGPointMake(frameTemp.origin.x+frameTemp.size.width/2,frameTemp.origin.y+frameTemp.size.height/2);20. NSDictionary*dict=NSDictionarydictionaryWithObjectsAndKeys:NSStringstringWithFormat:%f,point.x,x,NSStringstringWithFormat:%f,point.y,y,NSStringstringWithFormat:%d,i,num,nilnil;21. touchesArrayaddObject:dict;22. break;23. 24. 25. lineEndPoint=touchPoint;26. selfsetNeedsDisplay;27. 28. 從touchesArray中取得num對應(yīng)的值(button的tag值),并存入數(shù)組resultString中,手勢結(jié)束后調(diào)用方法:/手勢完成后調(diào)用的方法objcview plaincopy1. -(void)touchesEnded:(NSSet*)toucheswithEvent:(UIEvent*)event2. NSMutableString*resultString=NSMutableStringstring;for(NSDictionary*numintouchesArray)3. if(!numobjectForKey:num)break;4. resultStringappendString:numobjectForKey:num;5. if(style=1)6. success=rerificationDelegateverification:resultString;7. else8. success=resetDelegateresetPassword:resultString;9. for(inti=0;itouchesArray.count;i+)10. NSIntegerselection=touchesArrayobjectAtIndex:iobjectForKey:numintValue;GesturePasswordButton*buttonTemp=(GesturePasswordButton*)buttonArrayobjectAtIn11. dex:selection);12. buttonTempsetSuccess:success;buttonTempsetNeedsDisplay;13. 14. selfsetNeedsDisplay;15. 第一次輸入密碼時(shí),通過調(diào)用success = resetDelegate resetPassword:resultString;調(diào)轉(zhuǎn)到GesturePasswordController 中的- (BOOL)resetPassword:(NSString *)result方法進(jìn)行二次驗(yàn)證,以保存用戶密碼信息到本地objcview plaincopy1. -(BOOL)resetPassword:(NSString*)result2. if(previousStringisEqualToString:)3. previousString=result;4. gesturePasswordView.tentacleViewenterArgin;5. gesturePasswordView.statesetTextColor:UIColorcolorWithRed:2/255.fgreen:174/255.fblue:6. 240/255.falpha:1;7. gesturePasswordView.statesetText:請驗(yàn)證輸入密碼;8. returnYES;9. /保存密碼信息到本地else10. if(resultisEqualToString:previousString)11. KeychainItemWrapper*keychin=KeychainItemWrapperallocinitWithIdentifier:Gestur12. eaccessGroup:nil;13. keychinsetObject:forKey:(_bridgeid)kSecAttrAccount;14. keychinsetObject:resultforKey:(_bridgeid)kSecValueData;15. /selfpresentViewController:(UIViewController)animated:YEScompletion:nil;gesturePasswordView.statesetTextColor:UIColorcolorWithRed:2/255.fgreen:174/255.fblu16. e:240/255.falpha:1;17. gesturePasswordView.statesetText:已保存手勢密碼;selfdismissViewControllerAnimated:YEScompletion:18. ;19. returnYES;20. else21. previousString=;22. gesturePasswordView.statesetTextColor:UIColorredColor;gesturePasswordView.statesetText:兩次密碼不一致,請重新輸入;returnNO;23. 24. 通過self dismissViewControllerAnimated:YES completion:;關(guān)閉手勢密碼界面進(jìn)入后臺(tái),從后臺(tái)返回后調(diào)用方法:objcview plaincopy1. -(void)applicationWillEnterForeground:(UIApplication*)application2. /發(fā)送進(jìn)入前臺(tái)的通知3. NSNotificationCenterdefaultCenterpostNotificationName:yesobject:nil;4. NSLog(333333333)

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論