iOS学习笔记-064.手势解锁

手势解锁

一、代码

1.WMLockerView.m

//
//  ViewController.m
//  03_UIView55_手势解锁
//
//  Created by 杞文明 on 16/4/19.
//  Copyright © 2016年 杞文明. All rights reserved.
//

#import "ViewController.h"
#import "WMLockerView.h"

@interface ViewController ()<WMLockerViewDelegate>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (void)lockViewDidClick:(WMLockerView *)lockView andPwd:(NSString *)pwd{
    NSLog(@"结果是:%@",pwd);
}

@end

2.WMLockerView.m

//
//  WMLockerView.m
//  03_UIView55_手势解锁
//
//  Created by 杞文明 on 2016/04/19 08:20:20   星期二
//  Copyright © 2016年 杞文明. All rights reserved.
//

#import "WMLockerView.h"

@interface  WMLockerView()

@property (nonatomic, strong) NSMutableArray *buttons;
@property (nonatomic, assign) CGPoint currentPoint;

@end

@implementation WMLockerView

- (NSMutableArray *)buttons
{
    if (_buttons == nil) {
        _buttons = [NSMutableArray array];
    }
    return _buttons;
}

/**
 * 使用代码创建的时候都会调用这个方法
 */
- (instancetype)initWithFrame:(CGRect)frame{

    if(self = [super initWithFrame:frame]){
        //创建9宫格
        [self setup];
    }
    return self;
}

/**
 * 使用xib或storyboard创建的时候都会调用这个方法
 */
- (instancetype)initWithCoder:(NSCoder *)aDecoder{
    if(self = [super initWithCoder:aDecoder]){
        //创建9宫格
        [self setup];
    }
    return self;
}

/**
 * 创建9宫格
 */
-(void)setup{
    //    //总个数据
    //    int count = oneRowCount*oneRowCount;

    //一行中个个数 就是多少成多少
    int oneRowCount = 3;
    int btnW = 74;
    int btnH = 74;
    int btnX = 0 ;//循环中会计算
    int btnY = 0 ;

    //间距
    int margin = ( self.bounds.size.width-(btnH*oneRowCount) )/(oneRowCount+1);

    for (int i=0; i<oneRowCount; i++) {
        for (int j=0; j<oneRowCount; j++) {
            //计算x和y
            btnX = margin + i*(margin+btnW);
            btnY = margin + j*(margin+btnH);
            //画控件
            UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
            [btn setBackgroundImage:[UIImage imageNamed:@"gesture_node_normal"] forState:UIControlStateNormal];
            [btn setBackgroundImage:[UIImage imageNamed:@"gesture_node_highlighted"] forState:UIControlStateSelected];
            //屏蔽触摸事件 禁止与用户交互
            btn.userInteractionEnabled = NO;
            //设置位置
            [btn setFrame:CGRectMake(btnX, btnY, btnW, btnH)];
            //设置对应的密码值
            btn.tag = (i+1)*10 + (j+1);
            //添加到view中
            [self addSubview:btn];
        }
    }
}

/**
 * 开始触摸
 */
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    //1.获取点
    CGPoint point = [self getCurrentTouchPoint:touches];

    //2.获取点所在的按钮
    UIButton * btn = [self getCurrentBtnWithPoint:point];

    //3.如果按钮不为空,那么我就就加入到集合中,并高亮显示
    if(btn){
        btn.selected = YES;
        [self.buttons addObject:btn];
    }
}

/**
 *移动
 */
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    //1.获取点
    CGPoint point = [self getCurrentTouchPoint:touches];

    //2.获取点所在的按钮
    UIButton * btn = [self getCurrentBtnWithPoint:point];

    //3.如果按钮不为空,那么我就就加入到集合中,并高亮显示
    if(btn && btn.selected != YES){
        btn.selected = YES;
        [self.buttons addObject:btn];
    }

    //4.记录当前的点
    self.currentPoint =point;

    //5.通知视图重新绘制
    [self setNeedsDisplay];
}

/**
 *抬起
 */
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

    //0.告诉代理密码
    //0.1获取密码
    NSMutableString * result = [NSMutableString string];
    for (UIButton *btn in self.buttons) {
        [result appendFormat:@"%ld",btn.tag];
    }
    //0.2 告诉代理密码
    if ([self.delegate respondsToSelector:@selector(lockViewDidClick:andPwd:)]) {
        [self.delegate lockViewDidClick:self andPwd:result];
    }

    //1.删除所有的按钮的选中状态
//    [self.buttons makeObjectsPerformSelector:@selector(setSelected:) withObject:@(NO)];
    for (UIButton *btn in self.buttons) {
        btn.selected = NO;
    }

    //2.删除集合中的按钮
    [self.buttons removeAllObjects];

    //3.清空当前点
    self.currentPoint = CGPointZero;

    //4.重新绘制
    [self setNeedsDisplay];
}


/**
 *绘画
 */
- (void)drawRect:(CGRect)rect {
    //绘制其实就是绘制线
    //1.获取上线文
    CGContextRef ctx  = UIGraphicsGetCurrentContext();

    //2.清空上下文  这是因为,如果背景色是默认的,它会保存之前的绘图,这样界面上就会乱的
//    CGContextClearRect(ctx, rect);

    //3.绘制点
    for (int i=0; i<self.buttons.count; i++) {
        //取出Button
        UIButton * btn = self.buttons[i];
        if (i==0) {
            CGContextMoveToPoint(ctx, btn.center.x, btn.center.y);
        } else {
            CGContextAddLineToPoint(ctx, btn.center.x, btn.center.y);
        }
    }
    //4.当前绘制(和手指一起移动),如果这个没有按钮,那么绘制,不然回报没有起始点的错误
    if (self.buttons.count>0) {
        CGContextAddLineToPoint(ctx, self.currentPoint.x,self.currentPoint.y);
    }

//    NSLog(@"count %ld",self.buttons.count);

    //5.设置线条的一些属性
    [[UIColor greenColor]set];
    CGContextSetLineWidth(ctx, 10);
    CGContextSetLineJoin(ctx, kCGLineJoinRound);
    CGContextStrokePath(ctx);
}

/**
 *  根据系统传入的UITouch集合获取当前触摸的点
 *  @return 当初触摸的点
 */
- (CGPoint)getCurrentTouchPoint:(NSSet *)touches
{
    // 1.获取按下的点
    UITouch *touch =  [touches anyObject];
    CGPoint point = [touch locationInView:touch.view];
    return point;
}

/**
 *  根据触摸点获取触摸到的按钮
 *  @return 触摸的按钮
 */
- (UIButton *)getCurrentBtnWithPoint:(CGPoint)point
{
    // 2.判断触摸的位置是否在按钮的范围内
    for (UIButton *btn in self.subviews) {
        //判断点在不在这个按钮内
        if (CGRectContainsPoint(btn.frame, point)) {
            return btn;
        }
    }
    return nil;
}



@end

二、图示

这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值