图案解锁

作者: 小石头呢 | 来源:发表于2019-03-02 11:47 被阅读30次

    源代码:

    链接:
    https://pan.baidu.com/s/1NALgHczHTRgZwDyHWE6MLQ 密码:xgd6

    主要代码:

    
    #import "ViewController.h"
    
    @interface ViewController ()
    
    //不想被外部访问的信息
    
    /**定义一个保存九个点的可变数组*/
    @property (nonatomic,strong) NSMutableArray *dotViewArray;
    
    /**定义一个保存所有线的Tag值得可变数组*/
    @property (nonatomic,strong) NSMutableArray *lineTagsArray;
    
    /**定义一个保存所有被选中的点或线的可变数组*/
    @property (nonatomic,strong) NSMutableArray *selectedViewsArray;
    
    /**保存上一个被点亮的点的Tag值*/
    @property (nonatomic,assign) NSInteger lastSelectedDotTag;
    
    /**定义一个提示接下来的操作的文本框*/
    @property (nonatomic,strong) UILabel *hintLabel;
    
    /**记录密码*/
    @property (nonatomic,strong) NSMutableString *pwdString;
    
    /**定义一个变量存储密码*/
    @property (nonatomic,strong) NSString *password;
    
    /**定义一个变量存储第一次设置的密码*/
    @property (nonatomic,strong) NSString *firstPassword;
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        
        //初始化
        [self arrayAndStringInit];
        
        //界面布局
        [self UIInit];
        
    }
    
    #pragma mark -------creatImageViewWithFrame ---------
    //创建UIImageView呈现图片
    - (UIImageView *)creatImageViewWithFrame:(CGRect) frame name:(NSString *)imageName{
        
        //创建
        UIImageView *imageView = [[ UIImageView alloc] initWithFrame:frame];
        //添加图片
        imageView.image = [UIImage imageNamed:imageName];
        //不显示
        imageView.hidden = YES;
        //添加
        [self.view addSubview:imageView];
        
        return imageView;
    }
    
    #pragma mark -------arrayAndStringInit ---------
    //数组初始化
    - (void)arrayAndStringInit{
        
        //初始化存放点的数组
        self.dotViewArray = [NSMutableArray arrayWithCapacity:9];
        
        //初始化存放Tag值得数组
        self.lineTagsArray = [NSMutableArray array];
        
        //初始化存放被选中的点或者线
        self.selectedViewsArray = [NSMutableArray array];
        
        //初始化字符串
        self.pwdString = [NSMutableString string];
        
    }
    
    #pragma mark -------UIInit ---------
    //界面布局
    - (void)UIInit{
        
        //创建背景图片
        {
            UIImageView *bgView = [self creatImageViewWithFrame:self.view.bounds name:@"DotLock_Normal"];
            bgView.contentMode = UIViewContentModeScaleAspectFit;
            //不隐藏
            bgView.hidden = NO;
        }
        
        //添加操作图片
        {
            //添加九个点
            [self addDot];
        
            //添加六条横线
            [self addTransverseLine];
        
            //添加六条竖线
            [self addVerticalLine];
        
            //添加八条斜线
            [self addSlantLine];
        }
        
        //提示框布局
        {
            //创建
            self.hintLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 124, self.view.frame.size.width, 50)];
        
            //文本
            // _hintLabel.text = @"请绘制密码";
            
            //字体颜色
            _hintLabel.textColor = [UIColor blackColor];
            
            //对齐方式
            _hintLabel.textAlignment = NSTextAlignmentCenter;
            
            //添加
            [self.view addSubview:_hintLabel];
            
        }
        
        //密码操作
        {
            //获取密码
            //[[NSUserDefaults standardUserDefaults] setObject:@"" forKey:@"password"];
            self.password = [[NSUserDefaults standardUserDefaults] objectForKey:@"password"];
            
            if (_password.length == 0) {
                //设置密码
                _hintLabel.text = @"请设置解锁图案";
            } else {
                //输入密码
                _hintLabel.text = @"请绘制解锁图案";
            }
            
        }
        
    }
    
    #pragma mark -------addDot ---------
    //添加九个点
    - (void)addDot{
        
        int index = 1;
        
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                
                //循环创建九个点
                UIImageView *dotImageView = [self creatImageViewWithFrame:CGRectMake(31+j*98, 214+i*97, 62, 62) name:@"DotLock_Selected"];
                //dotImageView.hidden = NO;
                
                //设置Tag值
                dotImageView.tag = index;
                index++;
                
                //放到存放点的数组中
                [self.dotViewArray addObject:dotImageView];
            }
        }
        
    }
    
    
    #pragma mark -------addTransverseLine ---------
    //添加横线
    //12 23 45 56 78 89
    -(void)addTransverseLine{
        
        int index = 12;
        
        for (int i = 0; i < 6; i++) {
            
            //循环创建六条横线
            UIImageView *transverseImageView = [self creatImageViewWithFrame:CGRectMake(90+i%2*97, 233+i/2*99, 38, 22) name:@"DotLock_Normal_Highlight1"];
            //transverseImageView.hidden = NO;
            
            //赋予Tag值
            transverseImageView.tag = index;
            
            //将Tag值添加到数组中
            [self.lineTagsArray addObject:@(index)];
            
            if ((i+1)%2 == 0 && i != 0) {
                
                index = index + 11;
                
            }
            
            index = index + 11;
            
        }
        
    }
    
    #pragma mark -------addVerticalLine ---------
    //添加竖线
    //14 25 36 47 58 69
    -(void)addVerticalLine{
        
        int index = 14;
        
        for (int i = 0; i < 6; i++) {
            
            //循环创建六条竖线
            UIImageView *verticalImageView = [self creatImageViewWithFrame:CGRectMake(51+i%3*99, 273+i/3*98, 20, 38) name:@"DotLock_Normal_Highlight2"];
            //verticalImageView.hidden = NO;
            
            //赋予Tag值
            verticalImageView.tag = index;
            
            //将Tag值添加到数组中
            [self.lineTagsArray addObject:@(index)];
            
            index = index + 11;
            
        }
    }
    
    #pragma mark -------addSlantLine ---------
    //添加斜线
    //右斜 24 35 57 68
    //左斜 15 26 48 59
    -(void)addSlantLine{
        
        int indexR = 15;
        
        int indexL = 24;
        
        for (int i = 0; i < 4; i++) {
            
            //循环创建四条右斜线
            UIImageView *rSlantLine = [self creatImageViewWithFrame:CGRectMake(73+i%2*99, 255+i/2*99, 64, 64) name:@"DotLock_Normal_Highlight3"];
            //rSlantLine.hidden = NO;
            
            //赋予Tag值
            rSlantLine.tag = indexR;
            
            //将Tag值添加到数组中
            [self.lineTagsArray addObject:@(indexR)];
            
            indexR = indexR +11;
            
            //循环创建四条左斜线
            UIImageView *lSlantLine = [self creatImageViewWithFrame:CGRectMake(82+i%2*99, 255+i/2*99, 64, 64) name:@"DotLock_Normal_Highlight4"];
            //lSlantLine.hidden = NO;
            
            //赋予Tag值
            lSlantLine.tag = indexL;
            
            //将Tag值添加到数组中
            [self.lineTagsArray addObject:@(indexL)];
            
            indexL = indexL +11;
            
            if (i + 1 == 2) {
                indexL = indexL + 11;
                indexR = indexR + 11;
            }
            
        }
    }
    
    #pragma mark -------touchesBegan ---------
    //开始触摸
    -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
        
        //获取触摸点的坐标
        UITouch *touch = [touches anyObject];
        CGPoint location = [touch locationInView:self.view];
        
        //判断触摸点有没有在摸一个圆点上
        for (UIImageView *dotView in _dotViewArray) {
            
            //判断
            if (CGRectContainsPoint(dotView.frame, location)) {
                
                //点亮
                dotView.hidden = NO;
                
                //记录这个点的Tag
                _lastSelectedDotTag = dotView.tag;
                
                //保存点亮的点
                [_selectedViewsArray addObject:dotView];
                
                //保存密码 拼接字符串
                [self.pwdString appendFormat:@"%ld", (long)dotView.tag];
                
            }
        }
    }
    
    #pragma mark -------touchesMoved ---------
    //滑动时
    -(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
        
        //获取触摸点的坐标
        UITouch *touch = [touches anyObject];
        CGPoint location = [touch locationInView:self.view];
        
        //判断触摸点有没有在摸一个圆点上
        for (UIImageView *dotView in _dotViewArray) {
            
            //判断
            if (CGRectContainsPoint(dotView.frame, location)) {
                
                //如果这个点没有被点亮
                if (dotView.hidden == YES) {
                    
                    //是第一个点
                    if (_lastSelectedDotTag == 0) {
                        
                        //直接点亮
                        dotView.hidden = NO;
                        
                        //记录这个点的Tag
                        _lastSelectedDotTag = dotView.tag;
                        
                        //保存点亮的点
                        [_selectedViewsArray addObject:dotView];
                        
                        //保存密码 拼接字符串
                        [self.pwdString appendFormat:@"%ld", (long)dotView.tag];
                        
                    } else {//不是第一个点
                        
                        //判断上一个点和当前这个点之间有没有直接的线路
                        //获取上一个点和当前这个点的Tag组成的最小两位数
                        NSInteger lineTag = _lastSelectedDotTag > dotView.tag?dotView.tag*10+_lastSelectedDotTag:_lastSelectedDotTag*10+dotView.tag;
                        NSLog(@"%ld",(long)lineTag);
                        
                        //数组里面有这条线
                        if ([_lineTagsArray containsObject:@(lineTag)]) {
                            
                            //点亮
                            dotView.hidden = NO;
                            
                            //点亮线
                            UIImageView *lineImageView = [self.view viewWithTag:lineTag];
                            lineImageView.hidden = NO;
                            
                            //记录这个点的Tag
                            _lastSelectedDotTag = dotView.tag;
                            
                            //保存点亮的点
                            [_selectedViewsArray addObject:dotView];
                            
                            //保存密码 拼接字符串
                            [self.pwdString appendFormat:@"%ld", (long)dotView.tag];
                            
                            //保存点亮的线
                            [_selectedViewsArray addObject:lineImageView];
                            
                        }
                        
                    }
                    
                }
                
            }
        }
    }
    
    #pragma mark -------touchesEnded ---------
    //结束触摸
    -(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
        
        //设置密码
        if (_password.length == 0) {
            
            //第一次
            if (_firstPassword.length == 0) {
                
                //保存刚才输入的密码
                self.firstPassword = [NSString stringWithString:_pwdString];
                
                _hintLabel.text = @"请确认密码图案";
                
            } else {//第二次
                
                //设置成功
                if ([_firstPassword isEqualToString:_pwdString]) {
                    
                    self.hintLabel.text = @"设置成功";
                    
                    //保存密码
                    [[NSUserDefaults standardUserDefaults] setObject:_pwdString forKey:@"password"];
                    
                    
                    
                } else {
                    
                    //不一致 重新设置
                    self.hintLabel.text = @"两次图案不一致 请重新绘制";
                    //self.firstPassword = @"";
                    
                }
                
            }
            
        } else {//解锁
            
            //密码正确
            if ([self.pwdString isEqualToString:_password]) {
            
                
                self.hintLabel.text = @"解锁成功";
                
            
                
            } else {//密码错误
                
                
                self.hintLabel.text = @"密码错误";
                
            }
            
            
        }
        
        [self hideAllViews];
        
    }
    
    #pragma mark -------hideAllViews ---------
    //隐藏所有的视图
    -(void)hideAllViews{
        
        for (UIImageView *imgView in _selectedViewsArray) {
            //隐藏
            imgView.hidden = YES;
        }
        
        //清空
        [_selectedViewsArray removeAllObjects];
        _lastSelectedDotTag = 0;
        [_pwdString setString:@""];
        
        
    }
    
    
    
    @end
    

    运行结果

    运行结果

    附一:Tag值的应用

    通过给九个圆点赋予Tag值达到双重的好效果:第一我们可以通过点与点之间的Tag值确定线条的Tag值,从而通过线条的Tag值来;第二每个点的Tag值让我们可以通过记录Tag值来记录密码,从而进行相关的比较。

    附二:可变数组和可变字符串的使用

    使用之前一定要初始化

    附三:CGRectContainPoint

    CG_EXTERN bool CGRectContainsPoint(CGRect rect, CGPoint point)
        CG_AVAILABLE_STARTING(__MAC_10_0, __IPHONE_2_0);
    
    /* Return true if `rect2' is contained in `rect1', false otherwise. `rect2'
       is contained in `rect1' if the union of `rect1' and `rect2' is equal to
       `rect1'. */
    
    CG_EXTERN bool CGRectContainsRect(CGRect rect1, CGRect rect2)
        CG_AVAILABLE_STARTING(__MAC_10_0, __IPHONE_2_0);
    
    /* Return true if `rect1' intersects `rect2', false otherwise. `rect1'
       intersects `rect2' if the intersection of `rect1' and `rect2' is not the
       null rect. */
    

    附四:优化点

    设置密码时第二次密码与第一次不匹配或者解锁时密码错误之后,可以呈现图案变红,字体变红的效果。

    相关文章

      网友评论

        本文标题:图案解锁

        本文链接:https://www.haomeiwen.com/subject/kahsyqtx.html