解锁手势功能,显示痕迹和不显示痕迹只要看注释就可以

喜欢交朋友的加:微信号 dwjluck2013
#import "LoginGestureCodeViewController.h"
#import "UserModel.h"
#import "AppDelegate.h"
#import "LoginViewController.h"

#define BtnTag 5000

@interface LoginGestureCodeViewController ()

@property (nonatomic,strong)NSMutableArray *buttonArr;//全部手势按键的数组
@property (nonatomic,strong)NSMutableArray *selectorArr;//选中手势按键的数组
@property (nonatomic,assign)CGPoint startPoint;//记录开始选中的按键坐标
@property (nonatomic,assign)CGPoint endPoint;//记录结束时的手势坐标
@property (nonatomic,strong)UIImageView *imageView;//画图所需
@property (nonatomic,strong)NSMutableString *handMoveString;//最终手势的字符串
@end

@implementation LoginGestureCodeViewController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationItem.title = @"验证手势";
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = getColor(bgColor);
    
    if (!_buttonArr) {
        _buttonArr = [[NSMutableArray alloc] initWithCapacity:9];
    }
    
    self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64)];
    [self.view addSubview:self.imageView];
    
    //提示label
    UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT/5 - 40 / WIDTH_5S_SCALE, SCREEN_WIDTH, 14/ WIDTH_5S_SCALE)];
    textLabel.font = DEF_FontSize_14;
    textLabel.textColor = getColor(mainColor);
    textLabel.textAlignment = NSTextAlignmentCenter;
    textLabel.text = @"请输入手势密码";
    [self.view addSubview:textLabel];
    
    //创建按钮
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
            //设置TAG值
            if (i == 0) {
                btn.tag = BtnTag + j;
            }else if (i == 1){
                btn.tag = BtnTag + 3 + j;
            }else{
                btn.tag = BtnTag + 6 + j;
            }
            
            btn.frame = CGRectMake(SCREEN_WIDTH/6+SCREEN_WIDTH/4*j, SCREEN_HEIGHT/5+SCREEN_WIDTH/4*i, SCREEN_WIDTH/5, SCREEN_WIDTH/5);
            [btn setImage:[UIImage imageNamed:@"login_shoushi1"] forState:UIControlStateNormal];
            [btn setImage:[UIImage imageNamed:@"login_shoushi2"] forState:UIControlStateHighlighted];
            btn.imageView.contentMode = UIViewContentModeScaleAspectFit;
            btn.userInteractionEnabled = NO;
            [self.buttonArr addObject:btn];
            [self.imageView addSubview:btn];
        }
    }
    
    //确认按钮
    UIButton *commitButton = [[UIButton alloc] initWithFrame:CGRectMake(30 / WIDTH_5S_SCALE, SCREEN_HEIGHT - 64 - 40 - 50 / WIDTH_5S_SCALE, SCREEN_WIDTH - 60 / WIDTH_5S_SCALE, 40)];
    commitButton.titleLabel.font = DEF_FontSize_18;
    [commitButton setTitle:@"重置手势" forState:UIControlStateNormal];
    [commitButton setTitleColor:getColor(whiteColor) forState:UIControlStateNormal];
    [commitButton setBackgroundColor:getColor(mainColor)];
    commitButton.layer.cornerRadius = 5;
    commitButton.layer.masksToBounds = YES;
    [commitButton addTarget:self action:@selector(commitBtnAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:commitButton];
}

//画线
- (UIImage *)drawLine{
    UIImage *image = nil;
    UIColor *col = getColor(mainColor);
    
    //设置画图的大小为imageview的大小
    UIGraphicsBeginImageContext(self.imageView.frame.size);
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(context, 2);
    CGContextSetStrokeColorWithColor(context, col.CGColor);
    
    //设置画线起点
    CGContextMoveToPoint(context, self.startPoint.x, self.startPoint.y);
    
    //从起点画线到选中的按键中心,并切换画线的起点
    for (UIButton *btn in self.selectorArr) {
        CGPoint btnPo = btn.center;
        CGContextAddLineToPoint(context, btnPo.x, btnPo.y);
        CGContextMoveToPoint(context, btnPo.x, btnPo.y);
    }
    //画移动中的最后一条线
    CGContextAddLineToPoint(context, self.endPoint.x, self.endPoint.y);
    
    CGContextStrokePath(context);
    
    image = UIGraphicsGetImageFromCurrentImageContext();//画图输出
    UIGraphicsEndImageContext();//结束画线
    
    return image;
}

//开始手势
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    self.imageView.image = nil;
    self.selectorArr = nil;
    for (UIButton *btn in self.buttonArr) {
        btn.highlighted = NO;
    }
    UITouch *touch = [touches anyObject];
    if (touch) {
        //记录起点
        self.startPoint = [touch locationInView:self.imageView];
        
        for (UIButton *btn in self.buttonArr) {
            //记录按键坐标
            CGPoint po = [touch locationInView:btn];
            
            //判断按键坐标是否在手势开始范围内,是则为选中的开始按键
            if ([btn pointInside:po withEvent:nil]) {
                [self.selectorArr addObject:btn];
                btn.highlighted = YES;
                //保存起始坐标
                self.startPoint = btn.center;
            }
        }
    }
}

//移动中
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [touches anyObject];
    if (touch) {
        self.endPoint = [touch locationInView:self.imageView];
        
        for (UIButton *btn in self.buttonArr) {
            CGPoint po = [touch locationInView:btn];
            
            if ([btn pointInside:po withEvent:nil]) {
                BOOL isAdd = YES;//记录是否为重复按键
                for (UIButton *seBtn in self.selectorArr) {
                    if (seBtn == btn) {
                        isAdd = NO;//已经是选中过的按键,不再重复添加
                        break;
                    }
                }
                if (isAdd) {//未添加的选中按键,添加并修改状态
                    [self.selectorArr addObject:btn];
                    btn.highlighted = YES;
                }
            }
        }
    }
    
    //每次移动过程中都要调用这个方法,把画出的图输出显示
    self.imageView.image = [self drawLine];
    
}

//手势结束
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    //手势图画消失,打印已选择的数组
    self.imageView.image = nil;
    for (UIButton *btn in self.buttonArr) {
        btn.highlighted = NO;
    }
    self.startPoint = CGPointZero;
    NSMutableString *str = @"".mutableCopy;
    for (UIButton *btn in self.selectorArr) {
        NSLog(@"aaa -- %ld",btn.tag - BtnTag);
        str = (NSMutableString*)[str stringByAppendingString:[NSString stringWithFormat:@"%ld",btn.tag - BtnTag]];
    }
    self.handMoveString = str;
    
    // 注:可变代码
    NSString *locanHandMove = [UserModel sharedInstanced].handMove;
    if ([locanHandMove isEqualToString:str]) {
        AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication]delegate];
        [appDelegate loginStateChange];
    }else{
        ShowMessage(@"手势密码错误");
    }
    
//    //手势图画不消失,打印已选择的数组
//    self.startPoint = CGPointZero;
//    for (UIButton *btn in self.selectorArr) {
//        NSLog(@"aaa -- %ld",btn.tag - BtnTag);
//    }
}

#pragma mark -- btnAction

- (void)commitBtnAction{
    NSLog(@"确认按钮");
//    NSLog(@"--- %@",self.selectorArr);
//    for (UIButton *btn in self.selectorArr) {
//        NSLog(@"aaa -- %ld",btn.tag - BtnTag);
//    }
    //  切换根控制器(可变代码)
    UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:[[LoginViewController alloc]init]];
    [UIApplication sharedApplication].delegate.window.rootViewController = nav;
    
}


#pragma mark -- init

- (NSMutableArray *)selectorArr{
    if (!_selectorArr) {
        _selectorArr = [[NSMutableArray alloc] init];
    }
    return _selectorArr;
}

@end
原文地址:https://www.cnblogs.com/dujiahong/p/7604832.html