IOS 學習筆記 2015-04-15 手勢密碼(原),ios2015-04-15

來源:互聯網
上載者:User

IOS 學習筆記 2015-04-15 手勢密碼(原),ios2015-04-15

////  WPSignPasswordView.h//  網投網////  Created by wangtouwang on 15/4/9.//  Copyright (c) 2015年 wangtouwang. All rights reserved.//#import <UIKit/UIKit.h>@class  WPSignPasswordView;@protocol WPSignPasswordDelegate <NSObject>//設定密碼 確認成功@required-(void)setPawSuccess:(NSString *)password;//設定第一次臨時密碼成功@required-(void)setFirstPasswordSuccess:(NSString *)password;//第二次輸入確認密碼錯誤-(void)setTwoPasswordError;//修改手勢密碼 請輸入之前的密碼-(void)setSuccessAfterFirstPS:(NSString *)password;//進入程式後輸入手勢密碼判斷是否正確-(void)confirmPassword:(NSString *)password;//手勢密碼進入修改狀態(即原密碼輸入成功) 首次輸入-(void)updateSPFirst:(NSString *)password;//手勢密碼進入修改狀態(即原密碼輸入成功) 二次輸入,相當於輸入密碼確認-(void)updateSPConfirm:(NSString *)password;@end#pragma mark 手勢密碼View@interface WPSignPasswordView : UIView//設定代理@property(nonatomic,strong) id<WPSignPasswordDelegate> spDelegate;@end
////  WPSignPasswordView.m//  網投網////  Created by wangtouwang on 15/4/9.//  Copyright (c) 2015年 wangtouwang. All rights reserved.//#import "WPSignPasswordView.h"//螢幕的長寬#define KSCREEN_WIDTH [UIScreen mainScreen].bounds.size.width#define KSCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height@interface WPSignPasswordView(){    }@property(nonatomic,strong) NSMutableArray *stringArrays;@property(nonatomic,strong) NSMutableArray *allButtonsArray;//定義一個屬性,記錄當前點@property(nonatomic,assign)CGPoint currentPoint;@end@implementation WPSignPasswordView#pragma mark 執行個體化收集字串的數組 並且用懶載入-(NSMutableArray *)getStringArrays{    if (self.stringArrays==nil) {        self.stringArrays = [NSMutableArray array];    }    return  self.stringArrays;}#pragma mark 執行個體化包含所有密碼按鈕的數組 並且用懶載入-(NSMutableArray *)getAllButtonsArray{    if (self.allButtonsArray==nil) {        self.allButtonsArray = [NSMutableArray array];    }    return self.allButtonsArray;}#pragma mark 複寫初始化介面函數 initFrame-(instancetype)initWithFrame:(CGRect)frame{    self = [super initWithFrame:frame];    if (self) {        [self setup];    }    return self;}#pragma mark 介面布局-(void)setup{    //NSLog(@"初始化介面布局執行的");    for (int index=1; index<=9; index++) {        //建立按鈕        UIButton *numberButton = [[UIButton alloc] init];        //設定按鈕的背景圖片,並且設定是在何種狀態下        [numberButton setBackgroundImage:[UIImage imageNamed:@"gesture_node_normal"] forState:UIControlStateNormal];        [numberButton setBackgroundImage:[UIImage imageNamed:@"gesture_node_highlighted"] forState:UIControlStateSelected];        //將按鈕添加到視圖中        [self addSubview:numberButton];        //將按鈕儲存到按鈕數組中        [[self getAllButtonsArray] addObject:numberButton];        //禁止按鈕點擊事件        numberButton.userInteractionEnabled=NO;        //設定按鈕標誌值        numberButton.tag=index;            }}#pragma mark 看看何時觸發-(void)layoutSubviews{    //需要先調用父類的方法    [super layoutSubviews];    //設定按鈕位置    for (int index=0; index<self.allButtonsArray.count; index++) {        CGFloat inverst_top = KSCREEN_HEIGHT/4;        inverst_top=0;        UIButton *btn =(UIButton *) self.allButtonsArray[index];        //NSLog(@"%i",btn.tag);        //擷取行號        CGFloat row = index/3;        //擷取座標號        CGFloat loc = index%3;        // 按鈕長跟寬        CGFloat  btnHeight = 75;        CGFloat  btnWith=btnHeight;        //擷取按鈕間隔距離        CGFloat inverst = (KSCREEN_WIDTH-btnHeight*3)/4;        //X Y 座標        CGFloat btnX = inverst +loc*(btnWith+inverst);        CGFloat btnY = inverst + row*(btnHeight+inverst);        btn.frame=CGRectMake(btnX, btnY+inverst_top-15, btnHeight, btnHeight);    }}#pragma mark 複寫畫布布局- (void)drawRect:(CGRect)rect {    //NSLog(@"再次觸發嗎");    //擷取上下文    CGContextRef context = UIGraphicsGetCurrentContext();#pragma mark 填充畫布顏色    //填充上下文顏色    CGContextSetFillColorWithColor(context, [[UIColor grayColor] CGColor]);    //補充當前填充顏色的rect    CGContextFillRect(context, rect);#pragma mark -實現畫線功能    int i =0;    //繪圖(線段)    for (UIButton *btn in self.stringArrays) {        if (0==i) {            //設定起點(注意串連的是中點)            CGContextMoveToPoint(context, btn.center.x, btn.center.y);        }else{            CGContextAddLineToPoint(context, btn.center.x, btn.center.y);        }        i++;    }    //當所有按鈕的中點都串連好之後,再串連手指當前的位置    //判斷數組中是否有按鈕,只有有按鈕的時候才繪製    if (self.stringArrays.count !=0) {        //畫直線        CGContextAddLineToPoint(context, self.currentPoint.x, self.currentPoint.y);    }    //渲染    //設定線條的寬度    CGContextSetLineWidth(context, 10);    //設定映像上下文中的接接線的樣式。    CGContextSetLineJoin(context, kCGLineJoinRound);    //設定線條終點形狀    CGContextSetLineCap(context, kCGLineCapRound);    //畫筆顏色設定    CGContextSetRGBStrokeColor(context, 255/255.0, 100/255.0, 0/255.0, 1);    //開始繪製圖片    CGContextStrokePath(context);}#pragma mark 複寫 UIResponder--API 監聽手指移動開始-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{    UITouch *uitouch = [touches anyObject];    CGPoint point =  [uitouch locationInView:uitouch.view];    UIButton *targetBtn = nil;    for (UIButton *btn in self.allButtonsArray ) {        if (CGRectContainsPoint(btn.frame, point)) {            targetBtn = btn;            break;        }    }    if (targetBtn&&targetBtn.selected!=YES) {        targetBtn.selected=YES;        [[self getStringArrays] addObject:targetBtn];    }}#pragma mark 複寫 UIResponder--API 監聽手指移動-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{    UITouch *uitouch = [touches anyObject];    CGPoint movepoint =  [uitouch locationInView:uitouch.view];    UIButton *targetBtn = nil;    for (UIButton *btn in self.allButtonsArray ) {        if (CGRectContainsPoint(btn.frame, movepoint)) {            targetBtn = btn;            break;        }    }    if (targetBtn && targetBtn.selected != YES) {        //設定按鈕的選中狀態        targetBtn.selected=YES;        //把按鈕添加到數組中        [[self getStringArrays] addObject:targetBtn];    }    //記錄當前點(不在按鈕的範圍內)    self.currentPoint=movepoint;    //通知view重新繪製    [self setNeedsDisplay];}#pragma mark 複寫 UIResponder--API 監聽手指離開螢幕-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{    //取出使用者輸入的密碼 建立一個可變的字串,用來儲存使用者密碼 取出使用者輸入的密碼    NSMutableString *result=[NSMutableString string];    for (UIButton *btn in self.stringArrays) {        [result appendFormat:@"%lu",btn.tag];    }    //NSLog(@"使用者輸入的密碼為:%@",result);`    //清空連線記錄    [self.stringArrays makeObjectsPerformSelector:@selector(setSelected:) withObject:@(NO)];    //清空數組    [self.stringArrays removeAllObjects];    [self setNeedsDisplay];    //清空當前點    self.currentPoint=CGPointZero;        //擷取NSUserDefaults對象,判斷該對象中是否已存在手勢密碼    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];    BOOL flag = [userDefaults boolForKey:@"flag"];    NSInteger temp_signpaw = [userDefaults integerForKey:@"temp_signpaw"];    if (!flag) {#pragma mark -還未設定密碼        //記錄臨時密碼        if (temp_signpaw==0) {            if ([self.spDelegate respondsToSelector:@selector(setFirstPasswordSuccess:)]) {                [self.spDelegate setFirstPasswordSuccess:result];            }        }        //NSLog(@"%i",temp_signpaw);                if (temp_signpaw!=0&&temp_signpaw==[result integerValue]) {            //正確跳轉            if ([self.spDelegate respondsToSelector:@selector(setPawSuccess:)]) {                [self.spDelegate setPawSuccess:result];            }        }else if(temp_signpaw!=0&&temp_signpaw!=[result integerValue]){            //錯誤處理            if([self.spDelegate respondsToSelector:@selector(setTwoPasswordError)]){                [self.spDelegate setTwoPasswordError];            }        }    }else{#pragma mark -已設定密碼了        NSInteger status = [userDefaults integerForKey:@"status"];        if (status==-1) {            //調試狀態            NSLog(@"調試狀態");        }else{            //正常狀態            //判斷是從那個視窗跳轉過來的            NSString *page = [userDefaults objectForKey:@"turnPage"];            if ([page isEqualToString:@"mainPage"]) {                if ([self.spDelegate respondsToSelector:@selector(confirmPassword:)]) {                    [self.spDelegate confirmPassword:result];                }            }else{                NSInteger updateStatus = [userDefaults integerForKey:@"updateStatus"];                if (updateStatus==1) {                   NSInteger tempUpdate_signpaw = [userDefaults integerForKey:@"tempUpdate_signpaw"];                    if (tempUpdate_signpaw==0) {                        if ([self.spDelegate respondsToSelector:@selector(updateSPFirst:)]) {                            [self.spDelegate updateSPFirst:result];                        }                    }                    if(tempUpdate_signpaw!=0){                        if ([self.spDelegate respondsToSelector:@selector(updateSPConfirm:)]) {                            [self.spDelegate updateSPConfirm:result];                        }                    }                }                if(updateStatus!=1){                    //判斷是否為已成功輸入手勢密碼                    if ([self.spDelegate respondsToSelector:@selector(setSuccessAfterFirstPS:)]) {                        [self.spDelegate setSuccessAfterFirstPS:result];                    }                }            }        }    }}@end

 

 

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.