iOS 手势密码加锁

没有什么废话,直接上代码,需要的直接用。可以参考上一篇文章画图

#import "MYLineViewController.h"
#import "MyLine.h"
@interface MYLineViewController ()

@end

@implementation MYLineViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    MyLine *myline = [[MyLine alloc]initWithFrame:[UIScreen mainScreen].bounds];
    
    myline.backgroundColor = [UIColor whiteColor];
    [self.view addSubview: myline];
    
    [myline chuanZhi:^(NSString *str) {
        NSLog(@"%@",str);
    }];
}

在View界面

#import <UIKit/UIKit.h>
//一会要传值的类型
typedef void(^NewBlock)(NSString*);

@interface MyLine : UIView

//声明block的属性
@property (nonatomic) NewBlock MyBlock;

//声明block方法
- (void)chuanZhi:(NewBlock)block;

@end

#import "MyLine.h"
#define SCREEN_WIDTH   [UIScreen mainScreen].bounds.size.width
#define SCREENH_HEIGHT [UIScreen mainScreen].bounds.size.height

@interface MyLine ()
//记录选中按钮
@property (nonatomic)NSMutableArray *selectArray;
//记录当前手势所在点,用来划线
@property (nonatomic)CGPoint currentPoint;

@end

@implementation MyLine
//通过storyboard 或者 xib 文件创建的时候会调用这个方法
-(instancetype)initWithCoder:(NSCoder *)aDecoder
{
    if ([super initWithCoder:aDecoder])
    {
        _selectArray = [[NSMutableArray alloc]init];
        [self createButton];

    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame{
    if (self == [super initWithFrame:frame]) {
        _selectArray = [[NSMutableArray alloc]init];
        [self createButton];

    }
    return self;
}


//绘图
- (void)drawRect:(CGRect)rect {
    
    //获取绘图需要的上下文,他是专门用来保存绘画期间的数据的
    CGContextRef contextRef = UIGraphicsGetCurrentContext();
    
    if (self.selectArray.count == 0) {
        return;
    }
    
    UIBezierPath *path = [UIBezierPath bezierPath];
    //设置填充颜色
    [[UIColor blueColor] setFill];
    
    //设置描边颜色
    [[UIColor redColor] setStroke];
    
    //设置线宽
    CGContextSetLineWidth(contextRef, 10);
    //线的样式
    path.lineJoinStyle= kCGLineCapButt;
    //设置颜色
    [[UIColor colorWithRed:56/255.0 green:210/255.0 blue:254/255.0 alpha:0.5] set];
    //便利按钮  添加连线
    for (int i = 0; i < self.selectArray.count; i++) {
        UIButton *button = self.selectArray[i];
        if (i == 0) {
            //设置起点
            [path moveToPoint:button.center];
        }else{
            //添加连线
            [path addLineToPoint:button.center];
        }
    }
    
    //如果按钮不在当前点,就把当前触摸点作为当前点,可以使线随着拖动位置变化而变化
    [path addLineToPoint:_currentPoint];
    //把路径添加到上下文
    CGContextAddPath(contextRef, path.CGPath);
    //渲染
    CGContextStrokePath(contextRef);
}

//创建button
- (void)createButton{
    //左侧距离
    float leftW = (SCREEN_WIDTH - 250)/2;
    float heightW = 100;
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            if (i == 0) {
                button.tag = 100+j;
                button.frame = CGRectMake(leftW+100*j, heightW, 50, 50);
            }else if (i == 1){
                button.tag = 103+j;
                button.frame = CGRectMake(leftW+100*j, heightW+100, 50, 50);
            }else if (i == 2){
                button.tag = 106+j;
                button.frame = CGRectMake(leftW+100*j, heightW+200, 50, 50);
            }
            [button setBackgroundImage:[UIImage imageNamed:@"0.png"] forState:UIControlStateNormal];
            [button setBackgroundImage:[UIImage imageNamed:@"2.jpg"] forState:UIControlStateSelected];
            button.userInteractionEnabled = NO;
            
            [self addSubview:button];
        }
    }
    
    
}

 //根据触摸点找到对应的button
- (UIButton*)buttonWithPoint:(CGPoint)point{
    for (UIButton *button in self.subviews) {
        //注意这个:CGRectContainsPoint(btn.frame, point) 如果point这个点包含在btn的frame范围内
        if (CGRectContainsPoint(button.frame, point))
        {
            return button;
        }
    }
    return nil;
}


//开始触摸
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    //拿到触摸点
    UITouch *touch = [touches anyObject];
    CGPoint point = [touch locationInView:self];
    
    //根据触摸点找到对应的button
    UIButton *button = [self buttonWithPoint:point];
    
    if (button && (button.selected == NO)) {
        button.selected = YES;
        //把这个button添加到数组中
        [self.selectArray addObject:button];
    }
    
}

//触摸移动过程中
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    //拿到触摸点
    UITouch *touch = [touches anyObject];
    CGPoint point = [touch locationInView:self];
    
    //根据触摸点找到对应的button
    UIButton *button = [self buttonWithPoint:point];
    
    if (button && (button.selected == NO)) {
        button.selected = YES;
        //把这个button添加到数组中
        [self.selectArray addObject:button];
    }else{
        _currentPoint = point;
    }
    
    //调用绘图方法
    [self setNeedsDisplay];
}

//触摸结束
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    for (UIButton *button in self.subviews) {
        button.selected = NO;
    }

    NSMutableString *path = [[NSMutableString alloc]init];
    for (UIButton *button in _selectArray) {
        [path appendFormat:@"%ld",button.tag - 100];
    }
    if (self.MyBlock) {
        self.MyBlock(path);
    }
    [_selectArray removeAllObjects];
    [self setNeedsDisplay];
   
}
 
- (void)chuanZhi:(NewBlock)block{
    self.MyBlock = block;
}











@end

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,638评论 25 708
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,259评论 4 61
  • 一生与极,惟合于气.聚兮离兮,泰矣否矣.一意所觅,惟闲于忌.得其失其,厌已希已. 去去何追,戚戚何念.我因缘山,未...
    刘叔夜阅读 269评论 0 1
  • 《西厢记》王实甫 戏曲角色名称 末本:全剧以男主角主唱的剧本 正末:男主角 正旦:女主角 俫:仆伇 外:男女主角以...
    蝴蝶荣阅读 3,345评论 2 4
  • 今天,我和爸爸妈妈去爸爸学校了。中午吃的火锅鸡,可好吃了我吃了四个花卷儿,还吃了好多的鸡肉。然后爸爸给我买了两个乐...
    薛阔阅读 212评论 0 0