//
// ViewController.m// unlock
//
// Created by 蔡定龙 on 15-4-9.
// Copyright (c) 2015年 李灵杰. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@property (nonatomic,strong) NSMutableArray *dotViewsArray;//存放点的数组
@property (nonatomic,strong) NSMutableArray *selectedViewsArray;//存放已经选择的数组
@property (nonatomic,strong) NSMutableArray *allLinesArray;//存放线的数组
@property (nonatomic,assign) NSInteger lastSelectedDotTag;//存放上一个选择的点
@property (nonatomic,strong) UILabel *alertLabel;
@property (nonatomic,strong) NSMutableString *passwordString;
@property (nonatomic,strong) NSMutableString *inputPasswordString;
@property (nonatomic,strong) NSString *tempString;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self viewInitial];
[self hengxianView];
[self shuxianView];
[self xiexianView];
[self nineDotView];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)viewInitial{
self.dotViewsArray =[NSMutableArray arrayWithCapacity:9];
self.selectedViewsArray = [NSMutableArray arrayWithCapacity:20];
self.allLinesArray = [NSMutableArray arrayWithCapacity:20];
self.inputPasswordString = [NSMutableString string];
//底色背景
UIImage *bgImage = [UIImage imageNamed:@"Main_BG"];
self.view.backgroundColor = [UIColor colorWithPatternImage:bgImage];
//操作界面
[self createImageViewWihtFrame:CGRectMake(0, 50, 320, 460) andimageName:@"Unlock_DotLock1_Normal"];
[self createImageViewWihtFrame:CGRectMake(0, 25, 320, 47) andimageName:@"NavigationBar_BG"];
[self createImageViewWihtFrame:CGRectMake(100, 34, 120, 19) andimageName:@"NavigationBar_Logo"];
//NSUserDefaults 单例 用来保存少量的数据
//创建app时候就默认为我们建好了,使用standardUserDefaults获取这个对象
NSUserDefaults *userDefults = [NSUserDefaults standardUserDefaults];
//[userDefults setObject:nil forKey:@"password"];
/*
从本地读取密码
如果第一次运行找不到password对应的value
返回值就是nil
如果有密码 就返回密码
*/
self.passwordString = [userDefults objectForKey:@"password"];
//创建label
self.alertLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 130, 320, 50)];
//_alertLabel.text = @"请输入密码";
_alertLabel.textColor = [UIColor blackColor];
_alertLabel.textAlignment = NSTextAlignmentCenter;
[self.view addSubview:_alertLabel];
//确定提示文本的内容
//判断是否有密码
if (_passwordString.length==0) {
//no
_alertLabel.text = @"请绘制密码图案";
}else{
//yes
_alertLabel.text = @"请绘制解锁密码";
}
}
- (UIImageView *)createImageViewWihtFrame:(CGRect)rect andimageName:(NSString *)imageName{
UIImageView *tempImageView = [[UIImageView alloc]initWithFrame:rect];
tempImageView.image = [UIImage imageNamed:imageName];
[self.view addSubview:tempImageView];
return tempImageView;
}
- (void)nineDotView{
int dotTag=1;
for (int i=0; i<3; i++) {
for (int j=0; j<3; j++) {
CGRect rect = CGRectMake(35+ 99*j, 214 + 99*i, 52, 52);
UIImageView *dotView=[self createImageViewWihtFrame:rect andimageName:@"Unlock_DotLock1_Selected"];
dotView.hidden = YES;
dotView.tag = dotTag++;
[self.dotViewsArray addObject:dotView];//将所有的九个点加入存放点的数组
}
}
}
- (void)hengxianView{
int hengxianTag =12;
for (int i=0; i<3; i++) {
for (int j=0; j<2; j++) {
CGRect rect = CGRectMake(43+ 99*j, 220 + 99*i, 120, 37);
UIImageView *hengxianView =[self createImageViewWihtFrame:rect andimageName:@"Unlock_DotLock1_Normal_Highlight1"];
hengxianView.hidden = YES;
hengxianView.tag = hengxianTag;
//将 tag值存入
//int 封装为NSNumber类型
NSNumber *tagNum = [NSNumber numberWithInt:hengxianTag];//但是tag值为int型,而数组只能存入对象型,故要转换下,将int封装为NSNumber类型
[_allLinesArray addObject:tagNum];//将横线的tag值存入所有存放线的数组
hengxianTag = hengxianTag+11;
}
hengxianTag = hengxianTag+11;
}
}
- (void)shuxianView{
int shuxianTag=14;
for (int i=0; i<2; i++) {
for (int j=0; j<3; j++) {
CGRect rect = CGRectMake(43+ 99*j, 220 + 99*i, 37, 120);
UIImageView *shuxianView=[self createImageViewWihtFrame:rect andimageName:@"Unlock_DotLock1_Normal_Highlight2"];
shuxianView.hidden = YES;
shuxianView.tag = shuxianTag;
NSNumber *tagNum = [NSNumber numberWithInt:shuxianTag];
[_allLinesArray addObject:tagNum];//同样将竖线的tag值存入所有存放线的数组
shuxianTag =shuxianTag+11;
}
}
}
- (void)xiexianView{
int rightTag=15;
int leftTag = 24;
for (int i=0; i<2; i++) {
for (int j=0; j<2; j++) {
CGRect rect1 = CGRectMake(43+ 99*j, 220 + 99*i, 120, 120);
UIImageView *rightView=[self createImageViewWihtFrame:rect1 andimageName:@"Unlock_DotLock1_Normal_Highlight3"];
CGRect rect2 = CGRectMake(50+ 99*j, 224 + 99*i, 120, 120);
UIImageView *leftView = [self createImageViewWihtFrame:rect2 andimageName:@"Unlock_DotLock1_Normal_Highlight4"];
rightView.hidden = YES;
leftView .hidden = YES;
rightView.tag=rightTag;
NSNumber *rightNum = [NSNumber numberWithInt:rightTag];
[_allLinesArray addObject:rightNum];//将斜线tag值存入所有存放线的数组
rightTag = rightTag +11;
leftView.tag =leftTag;
NSNumber *leftNum = [NSNumber numberWithInt:leftTag];
[_allLinesArray addObject:leftNum];//将斜线tag值存入所有存放线的数组
leftTag = leftTag +11;
NSLog(@"%lu",rightView.tag);
NSLog(@"%lu",leftView.tag);
}
rightTag = rightTag+11;
leftTag = leftTag+11;
}
}
/*
无论是判断线段能不能亮还是不相邻的点应不应该亮,都是需要通过两个点来判断的
线段能不能亮可以通过两个点来判断之间是否存在可以点亮的线段
而判断下一个点能不能亮可以判断之前的一个点与现在的点之间存不存在线段,如果不存在则不能亮
因此整个重点就在与如何获取上一个点和现在正在点击的点
在这个地方我们用一个lastSelected来存放上一个点击的点
*/
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
//得到触摸事件
UITouch *touch = [touches anyObject];
//得到触摸点坐标
CGPoint location = [touch locationInView:self.view];
for (UIImageView *dotView in _dotViewsArray) { // for in循环,将所有九个点遍历,然后通过touchbegan事件将开始的点点亮
if(CGRectContainsPoint(dotView.frame, location)){
dotView.hidden = NO;
[_selectedViewsArray addObject:dotView];//将began的第一个点放入selectedViewsArray,说明该点已经被选择
//记录当前这个点
_lastSelectedDotTag = dotView.tag;//并将这第一个点记录为上一个捕获的点
//捕获输入值进入输入密码字符串
[_inputPasswordString appendFormat:@"%ld",dotView.tag];
}
}
}
/*
但存在一个问题
如果began事件中,第一次点击的地方并不是九个点上,那么上一个捕获点tag值将为0
第二个捕获点到九个点上的话会触发一条线段点亮,
因此在捕获到第二个点的时候要判断前一个点的位置
*/
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
//得到触摸的事件
UITouch *touch = [touches anyObject];
//得到触摸的点得坐标
CGPoint location = [touch locationInView:self.view];
//判断到底是哪个点
for (UIImageView *dotView in _dotViewsArray) {
if (CGRectContainsPoint(dotView.frame, location)) {
if(_lastSelectedDotTag == 0){
dotView.hidden = NO;
[_selectedViewsArray addObject:dotView];//需要将第二个点放入被选择点数组
_lastSelectedDotTag = dotView.tag;//并将该点tag值放入上一个点位置中
[_inputPasswordString appendFormat:@"%ld",dotView.tag];
}else{
//获得当前这个点和lastTag组成点值
NSInteger lineTag = _lastSelectedDotTag>dotView.tag?dotView.tag*10+_lastSelectedDotTag:_lastSelectedDotTag*10+dotView.tag;//三项表达式,计算线的tag值
//判断组成的这条线是否存在
if ([_allLinesArray containsObject:[NSNumber numberWithInteger:lineTag]]) {//判断数组存在对象的方法
//cunzai
//判断点是否已经点亮
if (dotView.hidden == YES) {
dotView.hidden = NO;
//点亮线 tag -线的对象
UIImageView *lineView = [self.view viewWithTag:lineTag];//一个方法,通过tag值取view
lineView.hidden = NO;
//改变lastselectdot值
_lastSelectedDotTag = dotView.tag;//循环结束后将该点tag值继续置为上一个值
[_inputPasswordString appendFormat:@"%ld",dotView.tag];
[_selectedViewsArray addObject:dotView];//把点击的点放入已经选择的数组
[_selectedViewsArray addObject:lineView];//把点击的线放入已经选择的数组
}
}
}
}
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
//隐藏所有
for (UIImageView *ligthView in _selectedViewsArray) {
ligthView.hidden = YES;
}
//清空数组
[_selectedViewsArray removeAllObjects];
_lastSelectedDotTag = 0;//最后结束事件,将上一个捕获的点置0,为下次事件开始做准备
//操作,判断是否设置密码
if (_passwordString.length == 0) {
//第一次设置还是确认
if (_tempString.length == 0) {
//第一次设置密码
self.tempString = [NSString stringWithString:_inputPasswordString];
NSLog(@"第一次输入的密码%@",_tempString);
//提示再次输入密码
_alertLabel.text = @"请再次输入密码";
}else{
//第二次确认密码
//判断两次密码输入是否相同
if ([_tempString isEqualToString:_inputPasswordString]) {
/*
密码设置成功,将密码保存到本地的userDefaults
在判断字符串是否相同的时候,使用isEqualToString方法
切记不能写为
_tempString==_inputPasswordString
因为在这个地方,两个字符串表示的都是地址,因此地址判断等于地址传值
同样,在字符串对字符串赋值时用
stringWithString方法
*/
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject:_inputPasswordString forKey:@"password"];
NSLog(@"保存的密码为%@",_inputPasswordString);
//userDefaults是一个类似字典的方法,值和key需要配对存储
_alertLabel.text = @"设置密码成功";
}else{
//两次密码不一致
NSLog(@"验证的密码为%@",_inputPasswordString);
_alertLabel.text = @"图案错误,请重新绘制";
self.tempString = @"";
}
}
}else{
//设置过了,输入密码
if([_passwordString isEqualToString:_inputPasswordString]){
_alertLabel.text = @"解锁成功";
}else{
_alertLabel.text = @"图案错误,请重新绘制";
}
}
[_inputPasswordString setString:@""];
}
@end