iOS篇—Demo3—图案解锁

Demo3—图案解锁

一.搭建界面

1.设置背景颜色

self.view.backgroundColor = [UIColor grayColor];

2.设置操作图片

UIImageView *opBgImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, (self.view.frame.size.height-460/320.0*self.view.frame.size.width)*0.5+50, self.view.frame.size.width, 460/320.0*self.view.frame.size.width)];

opBgImageView.image = [UIImage imageNamed:@"Unlock_DotLock1_Normal"];

[self.view addSubview:opBgImageView];

3.写一个创界图片的方法

-(UIImageView *)createImageViewWithFrame:(CGRect)frame name:(NSString *)imageName{
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:frame];
    imageView.image = [UIImage imageNamed:imageName];
    [self.view addSubview:imageView];
    imageView.hidden = YES;
    return  imageView;
}

4.界面初始化logo

- (void)logo:(UIImageView *)imageView{
    UIImageView *logo = [self createImageViewWithFrame:CGRectMake((self.view.frame.size.width-80)/2, 110, 80, 80) name:@"timg.jpg"];
    [self.view addSubview:logo];
    logo.layer.cornerRadius = 40;
    logo.clipsToBounds = YES;
    logo.hidden = NO;
}

5.创建一个图片类用来管理正确错误两种状态的图片,默认情况下,视图处于隐藏状态

  • 写一个实例方法用来标记显示哪套图
- (void)changeImageWithStatus:(kImageViewStatus)status{
    if (status == kImageViewStatusNormal){
        self.image = [UIImage imageNamed:_normalImageName];
    } else{
        self.image = [UIImage imageNamed:_wrongImageName];
    }
}
  • 定义一个枚举变量标记图片状态
typedef enum{
    kImageViewStatusNormal,
    kImageViewStatusWrong
} kImageViewStatus;
  • 写一个类方法用来切换图片
+ (ChangeableImageView *)imageViewWithNormalImageName:(NSString *)normal andWrongImageName:(NSString *)wrong frame:(CGRect)frame father:(UIView *)contetView{
    ChangeableImageView *img = [[ChangeableImageView alloc] initWithFrame:frame];
    img.normalImageName = normal;
    img.wrongImageName = wrong;
    img.hidden = YES;
    img.image = [UIImage imageNamed:normal];
    [contetView addSubview:img];
    
    return img;
}

6.添加点视图

- (void)addLandDot:(UIImageView *)imageView{
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            ChangeableImageView *dotImageView = [ChangeableImageView imageViewWithNormalImageName:@"Unlock_DotLock1_Selected" andWrongImageName:@"Unlock_DotLock_Wrong1" frame:CGRectMake(50+127 * j, 418 + 127 * i, 60, 60) father:self.view];
        }
    }
}

7.添加线视图

  • 横线
- (void)addLandScapeLine{
    for (int i = 0; i < 6; i++) {
        ChangeableImageView *lineImageView = [ChangeableImageView imageViewWithNormalImageName:@"Unlock_DotLock1_Normal_Highlight1" andWrongImageName:@"Unlock_DotLock1_Wrong_Highlight1" frame:CGRectMake(72+i%2*128,424+i/2*128,120,37) father:self.view];
    }
}
  • 竖线
- (void)addPortraitLine{
    for (int i = 0; i < 6; i++) {
        ChangeableImageView *lineImageView = [ChangeableImageView imageViewWithNormalImageName:@"Unlock_DotLock1_Normal_Highlight2" andWrongImageName:@"Unlock_DotLock1_Wrong_Highlight2" frame:CGRectMake(59+i%3*128,425+i/3*128,37,120) father:self.view];
    }
}
  • 斜线
- (void)addDiagonaitLine{
    for (int i = 0; i < 4; i++) {
        //左斜线
        ChangeableImageView *LLineView = [ChangeableImageView imageViewWithNormalImageName:@"Unlock_DotLock1_Normal_Highlight4" andWrongImageName:@"Unlock_DotLock1_Wrong_Highlight4" frame:CGRectMake(90+i%2*128,440+i/2*128,122,122) father:self.view];
        
        //右斜线
        ChangeableImageView *RLineView = [ChangeableImageView imageViewWithNormalImageName:@"Unlock_DotLock1_Normal_Highlight3" andWrongImageName:@"Unlock_DotLock1_Wrong_Highlight3" frame:CGRectMake(65+i%2*128,440+i/2*128,122,122) father:self.view];
    }
}

二.点亮点、线

1.如何点亮点?使⽤用touch事件接收⽤用户的滑动操作,在滑动过程中可以获取触摸点的坐标x,y 判断这个触摸点是否在某⼀个点上,如果在,就点亮

  • 首先定义一个可变数组用于保存9个点的对象
@property(nonatomic, strong) NSMutableArray *nineDotViewArray;
  • 初始化可变数组
self.nineDotViewArray = [NSMutableArray array];
  • 在添加点视图方法里把九个点添加到数组中
- (void)addLandDot:(UIImageView *)imageView{
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            ChangeableImageView *dotImageView = [ChangeableImageView imageViewWithNormalImageName:@"Unlock_DotLock1_Selected" andWrongImageName:@"Unlock_DotLock_Wrong1" frame:CGRectMake(50+127 * j, 418 + 127 * i, 60, 60) father:self.view];
            //把创建的点视图加到数组中保存
            [self.nineDotViewArray addObject:dotImageView];
        }
    }
}
  • 添加触摸事件点亮点
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [touches anyObject];
    CGPoint location = [touch locationInView:self.view];
    
    for (UIImageView *dotView in self.nineDotViewArray){
         // CGRectContainsPoint 判断某一个矩形区域内是否包含某个点
        if (CGRectContainsPoint(dotView.frame, location)) {
            //如果在 点亮点
            dotView.hidden = NO;
            //记录最后点亮的点的tag值
            _lastSelectDotTag = dotView.tag;
            //记录密码,每调用一次此函数,就把点的tag存到字符串中
            [_password appendFormat:@"%ld", dotView.tag];
            //保存点亮的视图,即保存密码图案
            [_selectedViewArray addObject:dotView];
        }
    }
}

2.如何判断两个点之间的线是否能够被点亮?给每个点和线添加tag值,判断两个点组成的tag值有没有一条tag为这个值的线,有就是路径可达,没有就是不可达

  • 给点添加tag值
- (void)addLandDot:(UIImageView *)imageView{
    //给点添加tag值
    int count = 1;
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            ChangeableImageView *dotImageView = [ChangeableImageView imageViewWithNormalImageName:@"Unlock_DotLock1_Selected" andWrongImageName:@"Unlock_DotLock_Wrong1" frame:CGRectMake(50+127 * j, 418 + 127 * i, 60, 60) father:self.view];
            dotImageView.tag = count;
            count ++;
            //把创建的点视图加到数组中保存
            [self.nineDotViewArray addObject:dotImageView];
        }
    }
}
  • 给线添加tag值,并把所有线的tag值存入数组
@property(nonatomic, strong) NSMutableArray *allLineTagsArray;

初始化这个可变数组

self.allLineTagsArray = [NSMutableArray array];
/*
 添加横线
 横线的tag值:
 12 23
 45 56
 78 89
 */
- (void)addLandScapeLine{
    for (int i = 0; i < 6; i++) {
        ChangeableImageView *lineImageView = [ChangeableImageView imageViewWithNormalImageName:@"Unlock_DotLock1_Normal_Highlight1" andWrongImageName:@"Unlock_DotLock1_Wrong_Highlight1" frame:CGRectMake(72+i%2*128,424+i/2*128,120,37) father:self.view];

        if (i < 2) {
            lineImageView.tag = (i + 1) * 10 + (i + 2);
        }else if (i < 4){
            lineImageView.tag = (i + 2) * 10 + (i + 3);
        }else{
            lineImageView.tag = (i + 3) * 10 + (i + 4);
        }
        [self.allLineTagsArray addObject:@(lineImageView.tag)];
    }
}
/*
 添加竖线
 竖线的tag值:
 14 25 36
 47 58 69
 */
- (void)addPortraitLine{
    int count = 14;
    for (int i = 0; i < 6; i++) {
        ChangeableImageView *lineImageView = [ChangeableImageView imageViewWithNormalImageName:@"Unlock_DotLock1_Normal_Highlight2" andWrongImageName:@"Unlock_DotLock1_Wrong_Highlight2" frame:CGRectMake(59+i%3*128,425+i/3*128,37,120) father:self.view];

        lineImageView.tag = count;
        count = count +11;
        [self.allLineTagsArray addObject:@(lineImageView.tag)];
    }
}

/*
 添加斜线
 左斜线的tag值:
 24 35
 57 68
 右斜线的tag值:
 15 26
 48 59
 */
- (void)addDiagonaitLine{
    int count = 24;
    int count1 = 15;
    for (int i = 0; i < 4; i++) {
        //左斜线
        ChangeableImageView *LLineView = [ChangeableImageView imageViewWithNormalImageName:@"Unlock_DotLock1_Normal_Highlight4" andWrongImageName:@"Unlock_DotLock1_Wrong_Highlight4" frame:CGRectMake(90+i%2*128,440+i/2*128,122,122) father:self.view];
        LLineView.tag = LLineView.tag + count;
        if (i == 0||i == 2) {
            count = count +11;
        }
        if (i == 1) {
            count = count +22;
        }
        [self.allLineTagsArray addObject:@(LLineView.tag)];
        
        //右斜线
        ChangeableImageView *RLineView = [ChangeableImageView imageViewWithNormalImageName:@"Unlock_DotLock1_Normal_Highlight3" andWrongImageName:@"Unlock_DotLock1_Wrong_Highlight3" frame:CGRectMake(65+i%2*128,440+i/2*128,122,122) father:self.view];
        RLineView.tag = RLineView.tag + count1;
        if (i == 0||i == 2) {
            count1 = count1 +11;
        }
        if (i == 1) {
            count1 = count1 +22;
        }
        [self.allLineTagsArray addObject:@(RLineView.tag)];
    }
}
  • 计算两点组成的tag值(需要先记录最后一个点亮点的tag值)
@property(nonatomic, assign) NSInteger lastSelectDotTag;
NSInteger lineTag = _lastSelectDotTag > dotView.tag ? dotView.tag * 10 + _lastSelectDotTag : _lastSelectDotTag * 10 + dotView.tag;
  • 判断两点组成的tag值有没有一条tag为这个值的线,并点亮线
if ([_allLineTagsArray containsObject:@(lineTag)]) {
    //说明有路径  可以点亮点
    dotView.hidden = NO;
    //点亮线
    UIImageView *lineView = [self.view viewWithTag:lineTag];
    lineView.hidden = NO;
    //记录最后点亮的点的tag值
    _lastSelectDotTag = dotView.tag;
    //记录密码,每调用一次此函数,就把点的tag存到字符串中
    [_password appendFormat:@"%ld", dotView.tag];
}

三.密码及提示操作

1.如何保存密码图案?密码是字符串,用点的tag值来记录密码

  • 定义一个可变用来记录密码
@property(nonatomic, strong) NSMutableString *password;

初始化这个可变字符串

self.password = [NSMutableString string];
  • 定义一个变量用来保存密码
@property(nonatomic, strong) NSString *savePassword;
  • 定义一个变量用来保存设置密码中第一次输入的密码
@property(nonatomic, strong) NSString *firstPassword;

2.点亮点之后记录点亮点的tag值,记录密码

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [touches anyObject];
    CGPoint location = [touch locationInView:self.view];
    
    for (UIImageView *dotView in self.nineDotViewArray){
         // CGRectContainsPoint 判断某一个矩形区域内是否包含某个点
        if (CGRectContainsPoint(dotView.frame, location)) {
            //如果在 点亮点
            dotView.hidden = NO;
            //记录最后点亮的点的tag值
            _lastSelectDotTag = dotView.tag;
            //记录密码,每调用一次此函数,就把点的tag存到字符串中
            [_password appendFormat:@"%ld", dotView.tag];
        }
    }
}

3.提示操作

  • 定义一个label
@property(nonatomic, strong) UILabel *alertlabel;
  • 未开始绘制图案时label显示
	self.alertlabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 200, self.view.frame.size.width, 30)];
    _alertlabel.textAlignment = NSTextAlignmentCenter;
    _alertlabel.textColor = [UIColor whiteColor];
    [self.view addSubview:_alertlabel];
    
    //获取密码字符串
    if (_savePassword.length == 0) {
        //设置密码
        _alertlabel.text = @"请设置密码图案";
    }else{
        //输入密码
        _alertlabel.text = @"请绘制密码图案";
    }
  • 手指移开之后label相关提示
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    NSLog(@"%@", _password);
    
    //判断是设置还是解锁
    if (_savePassword.length == 0) {
        //设置或确认密码
        if (_firstPassword.length == 0) {
            //设置密码并保存设置的密码
            self.firstPassword = [NSString stringWithString:_password];
            //提示再次输入确认密码
            self.alertlabel.text =@"请确认密码图案";
            [self hideAllView];
        }else {
            //确认密码
            if ([_firstPassword isEqualToString:_password]) {
                self.alertlabel.text = @"设置成功";
                [self hideAllView];
                //保存密码
                [[NSUserDefaults standardUserDefaults]setObject:_password forKey:@"savePassword"];
            }else{
                self.alertlabel.text = @"两次图案不一致 请重新绘制";
                self.firstPassword = @"";
                [self showWrong];
            }
        }
    }else {
        //之前已经设置密码
        if ([self.password isEqualToString:_savePassword]) {
            //密码正确
            self.alertlabel.text = @"解锁成功";
        }else{
            //密码错误
            self.alertlabel.text = @"密码错误 请重新绘制";
            [self showWrong];
        }
    }

4.展示错误图片方法

- (void)showWrong{
    for (ChangeableImageView *imgView in _selectedViewArray) {
        [imgView changeImageWithStatus:kImageViewStatusWrong];
    }
    
    [self performSelector:@selector(hideAllView) withObject:nil afterDelay:1];
}

5.隐藏最后所有视图方法

- (void)hideAllView{
    for (ChangeableImageView *imgView in _selectedViewArray) {
        imgView.hidden = YES;
        [imgView changeImageWithStatus:kImageViewStatusNormal];
    }
    
    //清空
    [_selectedViewArray removeAllObjects];
    _lastSelectDotTag = 0;
    [_password setString:@""];
}

完整代码:

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

typedef enum{
    kImageViewStatusNormal,
    kImageViewStatusWrong
} kImageViewStatus;

@interface ChangeableImageView : UIImageView

@property (nonatomic, strong) NSString *normalImageName;
@property (nonatomic, strong) NSString *wrongImageName;

- (void)changeImageWithStatus:(kImageViewStatus)status;

+ (ChangeableImageView *)imageViewWithNormalImageName:(NSString *)normal andWrongImageName:(NSString *)wrong frame:(CGRect)frame father:(UIView *)contetView;
@end

NS_ASSUME_NONNULL_END
#import "ChangeableImageView.h"

@implementation ChangeableImageView

- (void)changeImageWithStatus:(kImageViewStatus)status{
    if (status == kImageViewStatusNormal){
        self.image = [UIImage imageNamed:_normalImageName];
    } else{
        self.image = [UIImage imageNamed:_wrongImageName];
    }
}

+ (ChangeableImageView *)imageViewWithNormalImageName:(NSString *)normal andWrongImageName:(NSString *)wrong frame:(CGRect)frame father:(UIView *)contetView{
    ChangeableImageView *img = [[ChangeableImageView alloc] initWithFrame:frame];
    img.normalImageName = normal;
    img.wrongImageName = wrong;
    img.hidden = YES;
    img.image = [UIImage imageNamed:normal];
    [contetView addSubview:img];
    
    return img;
}

@end
#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
//建立一个数组,保存九个点视图,不可以用copy,因为拷贝之后就变成不可变数组,不能给数组中添加对象
@property(nonatomic, strong) NSMutableArray *nineDotViewArray;
//建立一个数组,保存所有线的tag值,方便查找两点之间是否有这条线
@property(nonatomic, strong) NSMutableArray *allLineTagsArray;
//记录最后一个点亮点的tag值
@property(nonatomic, assign) NSInteger lastSelectDotTag;
//记录密码
@property(nonatomic, strong) NSMutableString *password;
//保存所有点亮的点和线
@property(nonatomic, strong) NSMutableArray *selectedViewArray;
//提示操作label
@property(nonatomic, strong) UILabel *alertlabel;
//保存原始密码
@property(nonatomic, strong) NSString *savePassword;
//保存设置密码中第一次输入的密码
@property(nonatomic, strong) NSString *firstPassword;
@end

#import "ViewController.h"
#import "ChangeableImageView.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //设置背景图片
//    UIImageView *bgImageView = [[UIImageView alloc]initWithFrame:self.view.bounds];
//    bgImageView.image = [UIImage imageNamed:@"Settings_PassCodeMode_BG"];
//    [self.view addSubview:bgImageView];
//    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"Settings_PassCodeMode_BG"]];
    
    //初始化属性变量
    self.nineDotViewArray = [NSMutableArray array];
    self.allLineTagsArray = [NSMutableArray array];
    self.password = [NSMutableString string];
    self.selectedViewArray = [NSMutableArray array];

    //设置背景颜色
    self.view.backgroundColor = [UIColor grayColor];
    
    //设置操作图片
    UIImageView *opBgImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, (self.view.frame.size.height-460/320.0*self.view.frame.size.width)*0.5+50, self.view.frame.size.width, 460/320.0*self.view.frame.size.width)];
    opBgImageView.image = [UIImage imageNamed:@"Unlock_DotLock1_Normal"];
    [self.view addSubview:opBgImageView];
    
    //创建label提示用户操作
    self.alertlabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 200, self.view.frame.size.width, 30)];
    _alertlabel.textAlignment = NSTextAlignmentCenter;
    _alertlabel.textColor = [UIColor whiteColor];
    [self.view addSubview:_alertlabel];
    
    //_password = @"12356 23";
    //NSLog(@"%@", _password);
    
    //获取密码字符串
    //self.savePassword = [[NSUserDefaults standardUserDefaults]objectForKey:@"password"];
    //NSLog(@"%d", _savePassword.length);
    if (_savePassword.length == 0) {
        //设置密码
        _alertlabel.text = @"请设置密码图案";
    }else{
        //输入密码
        _alertlabel.text = @"请绘制密码图案";
    }
    
    [self initUI];
}

//创建操作图片
-(void)initUI{
    //[self createImageViewWithFrame:CGRectMake(0, 160, 373, 490) name:@"Unlock_DotLock1_Normal"];
    UIImageView *imageView = [UIImageView new];
    UIImageView *logoView = [UIImageView new];
    [self logo:logoView];
    
    [self addLandScapeLine];
    [self addPortraitLine];
    [self addDiagonaitLine];
    [self addLandDot:imageView];
}

//创建一个UIImageView
-(UIImageView *)createImageViewWithFrame:(CGRect)frame name:(NSString *)imageName{
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:frame];
    imageView.image = [UIImage imageNamed:imageName];
    [self.view addSubview:imageView];
    imageView.hidden = YES;
    return  imageView;
}

//添加一个logo
- (void)logo:(UIImageView *)imageView{
    UIImageView *logo = [self createImageViewWithFrame:CGRectMake((self.view.frame.size.width-80)/2, 110, 80, 80) name:@"timg.jpg"];
    [self.view addSubview:logo];
    logo.layer.cornerRadius = 40;
    logo.clipsToBounds = YES;
    logo.hidden = NO;
}

/*
 添加9个点
 点的tag值:
 1 2 3 4 5 6 7 8 9
 */
- (void)addLandDot:(UIImageView *)imageView{
    //给点添加tag值
    int count = 1;
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            ChangeableImageView *dotImageView = [ChangeableImageView imageViewWithNormalImageName:@"Unlock_DotLock1_Selected" andWrongImageName:@"Unlock_DotLock_Wrong1" frame:CGRectMake(50+127 * j, 418 + 127 * i, 60, 60) father:self.view];

            //用一个temp接收创建的点视图
//            UIImageView *temp = [self createImageViewWithFrame:CGRectMake(48+115 * j, 313 + 115 * i, 50, 50) name:@"Unlock_DotLock1_Selected"];
            dotImageView.tag = count;
            count ++;
            //把创建的点视图加到数组中保存
            [self.nineDotViewArray addObject:dotImageView];
            //NSLog(@"%@", self.nineDotViewArray);
        }
    }
}

/*
 添加横线
 横线的tag值:
 12 23
 45 56
 78 89
 */
- (void)addLandScapeLine{
    for (int i = 0; i < 6; i++) {
        ChangeableImageView *lineImageView = [ChangeableImageView imageViewWithNormalImageName:@"Unlock_DotLock1_Normal_Highlight1" andWrongImageName:@"Unlock_DotLock1_Wrong_Highlight1" frame:CGRectMake(72+i%2*128,424+i/2*128,120,37) father:self.view];
//        UIImageView *temp = [self createImageViewWithFrame:CGRectMake(70+i%2*115,317+i/2*115,120,37) name:@"Unlock_DotLock1_Normal_Highlight1"];
        if (i < 2) {
            lineImageView.tag = (i + 1) * 10 + (i + 2);
        }else if (i < 4){
            lineImageView.tag = (i + 2) * 10 + (i + 3);
        }else{
            lineImageView.tag = (i + 3) * 10 + (i + 4);
        }
        [self.allLineTagsArray addObject:@(lineImageView.tag)];
        //NSLog(@"%ld", temp.tag);
    }
}

/*
 添加竖线
 竖线的tag值:
 14 25 36
 47 58 69
 */
- (void)addPortraitLine{
    int count = 14;
    for (int i = 0; i < 6; i++) {
        ChangeableImageView *lineImageView = [ChangeableImageView imageViewWithNormalImageName:@"Unlock_DotLock1_Normal_Highlight2" andWrongImageName:@"Unlock_DotLock1_Wrong_Highlight2" frame:CGRectMake(59+i%3*128,425+i/3*128,37,120) father:self.view];
//        UIImageView *temp = [self createImageViewWithFrame:CGRectMake(57+i%3*115,320+i/3*115,37,120) name:@"Unlock_DotLock1_Normal_Highlight2"];
        lineImageView.tag = count;
        count = count +11;
        [self.allLineTagsArray addObject:@(lineImageView.tag)];
        //NSLog(@"%ld", temp.tag);
    }
}

/*
 添加斜线
 左斜线的tag值:
 24 35
 57 68
 右斜线的tag值:
 15 26
 48 59
 */
- (void)addDiagonaitLine{
    int count = 24;
    int count1 = 15;
    for (int i = 0; i < 4; i++) {
        //左斜线
        ChangeableImageView *LLineView = [ChangeableImageView imageViewWithNormalImageName:@"Unlock_DotLock1_Normal_Highlight4" andWrongImageName:@"Unlock_DotLock1_Wrong_Highlight4" frame:CGRectMake(90+i%2*128,440+i/2*128,122,122) father:self.view];
//        UIImageView *temp = [self createImageViewWithFrame:CGRectMake(82+i%2*118,318+i/2*118,122,122) name:@"Unlock_DotLock1_Normal_Highlight4"];
        LLineView.tag = LLineView.tag + count;
        if (i == 0||i == 2) {
            count = count +11;
        }
        if (i == 1) {
            count = count +22;
        }
        [self.allLineTagsArray addObject:@(LLineView.tag)];
        //NSLog(@"%ld", temp.tag);
        
        //右斜线
        ChangeableImageView *RLineView = [ChangeableImageView imageViewWithNormalImageName:@"Unlock_DotLock1_Normal_Highlight3" andWrongImageName:@"Unlock_DotLock1_Wrong_Highlight3" frame:CGRectMake(65+i%2*128,440+i/2*128,122,122) father:self.view];
//        UIImageView *temp1 = [self createImageViewWithFrame:CGRectMake(55+i%2*118,318+i/2*118,122,122) name:@"Unlock_DotLock1_Normal_Highlight3"];
        RLineView.tag = RLineView.tag + count1;
        if (i == 0||i == 2) {
            count1 = count1 +11;
        }
        if (i == 1) {
            count1 = count1 +22;
        }
        [self.allLineTagsArray addObject:@(RLineView.tag)];
        //NSLog(@"%ld", temp1.tag);
    }
}

//点亮点和线
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [touches anyObject];
    CGPoint location = [touch locationInView:self.view];
    
    for (UIImageView *dotView in self.nineDotViewArray){
         // CGRectContainsPoint 判断某一个矩形区域内是否包含某个点
        if (CGRectContainsPoint(dotView.frame, location)) {
            //如果在 点亮点
            dotView.hidden = NO;
            //记录最后点亮的点的tag值
            _lastSelectDotTag = dotView.tag;
            //记录密码,每调用一次此函数,就把点的tag存到字符串中
            [_password appendFormat:@"%ld", dotView.tag];
            //保存点亮的视图,即保存密码图案
            [_selectedViewArray addObject:dotView];
        }
    }
}

- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [touches anyObject];
    CGPoint location = [touch locationInView:self.view];

    for (UIImageView *dotView in self.nineDotViewArray){
        // CGRectContainsPoint 判断某一个矩形区域内是否包含某个点
        if (CGRectContainsPoint(dotView.frame, location)) {
            //判断这个点是否已经被点亮
            if (dotView.hidden == YES) {
                //判断线能否被点亮 计算两点之间线的tag值
                NSInteger lineTag = _lastSelectDotTag > dotView.tag ? dotView.tag * 10 + _lastSelectDotTag : _lastSelectDotTag * 10 + dotView.tag;
                
                //判断计算出的tag值在不在数组中
                if ([_allLineTagsArray containsObject:@(lineTag)]) {
                    //说明有路径  可以点亮点
                    dotView.hidden = NO;
                    //点亮线
                    UIImageView *lineView = [self.view viewWithTag:lineTag];
                    lineView.hidden = NO;
                    //记录最后点亮的点的tag值
                    _lastSelectDotTag = dotView.tag;
                    //记录密码,每调用一次此函数,就把点的tag存到字符串中
                    [_password appendFormat:@"%ld", dotView.tag];
                    //保存点亮的视图,即保存密码图案
                    [_selectedViewArray addObject:dotView];
                    [_selectedViewArray addObject:lineView];
                }
            }
        }
    }
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    NSLog(@"%@", _password);
    
    //判断是设置还是解锁
    if (_savePassword.length == 0) {
        //设置或确认密码
        if (_firstPassword.length == 0) {
            //设置密码并保存设置的密码
            self.firstPassword = [NSString stringWithString:_password];
            //提示再次输入确认密码
            self.alertlabel.text =@"请确认密码图案";
            [self hideAllView];
        }else {
            //确认密码
            if ([_firstPassword isEqualToString:_password]) {
                self.alertlabel.text = @"设置成功";
                [self hideAllView];
                //保存密码
                [[NSUserDefaults standardUserDefaults]setObject:_password forKey:@"savePassword"];
            }else{
                self.alertlabel.text = @"两次图案不一致 请重新绘制";
                self.firstPassword = @"";
                [self showWrong];
            }
        }
    }else {
        //之前已经设置密码
        if ([self.password isEqualToString:_savePassword]) {
            //密码正确
            self.alertlabel.text = @"解锁成功";
        }else{
            //密码错误
            self.alertlabel.text = @"密码错误 请重新绘制";
            [self showWrong];
        }
    }
    
//    //清空
//    [self.password setString:@""];
//    //隐藏选中的视图
//    for (UIView *selectView in _selectedViewArray) {
//        selectView.hidden = YES;
//    }
//
//    //清空选中点的数组 重新绘制图案
//    [_selectedViewArray removeAllObjects];
    
}

- (void)showWrong{
    for (ChangeableImageView *imgView in _selectedViewArray) {
        [imgView changeImageWithStatus:kImageViewStatusWrong];
    }
    
    [self performSelector:@selector(hideAllView) withObject:nil afterDelay:1];
}

- (void)hideAllView{
    for (ChangeableImageView *imgView in _selectedViewArray) {
        imgView.hidden = YES;
        [imgView changeImageWithStatus:kImageViewStatusNormal];
    }
    
    //清空
    [_selectedViewArray removeAllObjects];
    _lastSelectDotTag = 0;
    [_password setString:@""];
}
@end

猜你喜欢

转载自blog.csdn.net/weixin_43733988/article/details/87997891