图案解锁

#import <UIKit/UIKit.h>
#import "CXLockView.h"


@interface CXLockViewController : UIViewController

@property (assign, nonatomic) LockMode mode;
@end
 
#import "CXLockViewController.h"

#define deviceWidth [UIScreen mainScreen].bounds.size.width
#define deviceHeight [UIScreen mainScreen].bounds.size.height
@interface CXLockViewController()<CXLockViewDelegate>
@end
@implementation CXLockViewController

-(void)viewDidLoad{
    self.view.backgroundColor = [UIColor lightGrayColor];
   
    CXLockView *lockView = [[CXLockView alloc] initWithFrame:CGRectMake(0, 0, deviceWidth, deviceWidth)];
   
    lockView.center = self.view.center;
    lockView.delegate = self;
    lockView.mode = self.mode;
    [self.view addSubview:lockView];
}

- (void) setPasswordFinished{
    NSLog(@"设置密码成功");
    [self.navigationController popViewControllerAnimated:YES];
}

-(BOOL) isPasswordMatch:(BOOL) flag{
//
    if (!flag) return NO;
   
    NSLog(@"解锁成功");
    [self.navigationController popViewControllerAnimated:YES];
   
    return YES;
   
}
 
 
 
 
#import <UIKit/UIKit.h>

typedef NS_ENUM(NSUInteger,LockMode){
//    设置密码
    LockModeSet,
//    解锁
    LockModeUnlock
};
//设定协议
@protocol CXLockViewDelegate <NSObject>
//设置密码
- (void) setPasswordFinished;
//判断解锁密码是否一致
- (BOOL) isPasswordMatch:(BOOL)flag;
@end

@interface CXLockView : UIView

@property (assign, nonatomic) LockMode mode;

@property (nonatomic, weak) id<CXLockViewDelegate> delegate;
@end
 
 
 
//  CXLockView.m
//  CXLockScreenDemo



#import "CXLockView.h"
#define   DEGREES_TO_RADIANS(degrees)  ((pi * degrees)/ 180)
#define pi 3.14159265359
@interface CXLockView()
//创建一个按钮集合
@property (strong, nonatomic) NSMutableArray *buttonArray;
//创建一个可变的字符串来接收解锁密码
@property (strong, nonatomic) NSMutableString *password;
@end


@implementation CXLockView
//给按钮初始化
-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self setup];
    }
    return self;
}

-(instancetype)initWithCoder:(NSCoder *)aDecoder{
    if (self = [super initWithCoder:aDecoder]) {
        [self setup];
    }
    return self;
}
//背景色 接收btn的数组
- (void) setup{
    self.backgroundColor = [UIColor clearColor];
    self.buttonArray = [NSMutableArray array];
    [self setupButton];
}


- (void) setupButton{
   
    for (int i = 0; i < 9 ; i ++) {
        UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
     
        [button setBackgroundImage:[UIImage imageNamed:@"circle_grey"] forState:UIControlStateNormal];
//        button被选中时的颜色
        [button setBackgroundImage:[UIImage imageNamed:@"circle_yellow"] forState:UIControlStateSelected];
//        用i的值设置为button的标签
        button.tag = i;
//        设置高亮
        button.adjustsImageWhenHighlighted = NO;
        //取消用户交互
        button.userInteractionEnabled = NO;
       
        [self addSubview:button];
       
    }
}

//制定按钮的布局
-(void)layoutSubviews{
    //个数
    int const col = 3;
    int const row = 3;
//    按钮的宽和高
    float width = 80.f;
    float height = 80.f;
//    间隙宽度
    float margin = (self.frame.size.width - (col * width)) / (col + 1);
//    jRow 行  iCol 列
      for (int jRow = 0; jRow < row; jRow++) {
        for (int iCol = 0; iCol < col; iCol++) {
//            button的位置以及大小
            UIButton * button = [self.subviews objectAtIndex:(iCol + jRow * col)];
            button.frame = CGRectMake(margin + iCol * (margin + width), margin + jRow * (margin + height), width, height);
        }
    }
}

//从点获取按钮
- (UIButton *) getButtonFromPoint:(CGPoint) point{
   
    for (UIButton *button in self.subviews) {
       
        if (CGRectContainsPoint(button.frame, point)) {
            return button;
        }
       
    }
    return nil;
}

//重置密码
- (void) reset{
    for (UIButton *button in self.subviews) {
        button.selected = NO;
    }
   
    [self.buttonArray removeAllObjects];
//    设置需要显示
    [self setNeedsDisplay];
//    设置密码
    if (self.mode == LockModeSet) {
        NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
//   设置关键字
        [user setObject:self.password forKey:@"password"];
//   是否实现代理方法
        if ([self.delegate respondsToSelector:@selector(setPasswordFinished)]) {
            [self.delegate setPasswordFinished];
        }
       
    }
   
    else if(self.mode == LockModeUnlock){
       
        NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
       
        NSString * password = [user objectForKey:@"password"];
       
        if ([password isEqualToString:self.password]) {
            if ([self.delegate respondsToSelector:@selector(isPasswordMatch:)]) {
                [self.delegate isPasswordMatch:YES];
            }
           
        }
       
        else
        {
            NSLog(@"密码不匹配");
        }
    }
}

//添加密码
- (void)getPassword{
    self.password = [NSMutableString string];
    for (UIButton *button in self.buttonArray) {
        [self.password appendString:[NSString stringWithFormat:@"%d",(int)button.tag]];
    }
    NSLog(@"%@",self.password);
}

#pragma mark - touch

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [touches anyObject];
   
    CGPoint point = [touch locationInView:self];
   
    UIButton *button = [self getButtonFromPoint:point];
   
    if (button) {
        button.selected = YES;
        [self.buttonArray addObject:button];
    }
}

-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [touches anyObject];
   
    CGPoint point = [touch locationInView:self];
   
    UIButton *button = [self getButtonFromPoint:point];
   
    if (button && button.selected != YES) {
        button.selected = YES;
        [self.buttonArray addObject:button];
    }
   
    [self setNeedsDisplay];
}

-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self getPassword];
    [self reset];
}


-(void)drawRect:(CGRect)rect{
//    Bezier Path  贝塞尔曲线路径
    UIBezierPath *path = [UIBezierPath bezierPath];
//    直线的宽度
    path.lineWidth = 5.f;
   
    path.lineCapStyle = kCGLineCapRound;//线条拐角
    path.lineJoinStyle = kCGLineJoinRound;//终点处理
   
    if (self.buttonArray.count) {
       
//        绘制图案
        for (int i = 0;  i < self.buttonArray.count; i++) {
            UIButton *button = self.buttonArray[i];
            if (0 == i) {
//                设置初始线段的起点
                [path moveToPoint:button.center];
            }else{
//                创建一个形状的线段
                [path addLineToPoint:button.center];
            }
        }
        [[UIColor redColor] set];//设置线条颜色
//        用当前画笔描绘一个路径的轮廓。打开的图形不会被这个函数关闭
//        [path stroke];//根据坐标点连线
       
        [path fill];//根据坐标点连线并填充
       
    }
   

   
}

@end
 
 
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
   
    ViewController * vc = [[ViewController alloc] init];
    UINavigationController * con = [[UINavigationController alloc] initWithRootViewController:vc];
    con.navigationBarHidden = YES;
    self.window.rootViewController = con;
    [self.window makeKeyAndVisible];
    return YES;
 
 
 
#import "ViewController.h"
#import "CXLockViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
//    背景色
    self.view.backgroundColor = [UIColor whiteColor];
//    设置密码按钮
    UIButton * setPassBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    [setPassBtn setTitle:@"设置密码" forState:UIControlStateNormal];
    setPassBtn.frame = CGRectMake(200, 100, 100, 100);
    [setPassBtn addTarget:self action:@selector(touch1) forControlEvents:UIControlEventTouchUpInside];
   
    [self.view addSubview:setPassBtn];
//    解锁按钮
    UIButton * UnlockBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    [UnlockBtn setTitle:@"解锁" forState:UIControlStateNormal];
    UnlockBtn.frame = CGRectMake(200, 300, 100, 100);
    [UnlockBtn addTarget:self action:@selector(touch2) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:UnlockBtn];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//设置密码
- (void) touch1{
   
    CXLockViewController *vc = [[CXLockViewController alloc] init];
    vc.mode = LockModeSet;
   
    [self.navigationController pushViewController:vc animated:YES];
}
//解锁
- (void) touch2{
    CXLockViewController *vc = [[CXLockViewController alloc] init];
    vc.mode = LockModeUnlock;
   
    [self.navigationController pushViewController:vc animated:YES];
}
@end
原文地址:https://www.cnblogs.com/tianlianghong/p/5357089.html