手勢密碼源碼

//
//  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
相關文章
相關標籤/搜索