手势解锁

#import "LockView.h"

@interface LockView ()
@property(nonatomic,assign)CGPoint curtentP;
@property(nonatomic,retain)NSMutableArray * selectedsBtn;
@end
@implementation LockView
-(NSMutableArray*)selectedsBtn
{
    if (!_selectedsBtn)
    {
        _selectedsBtn=[NSMutableArray array];
    }
    return _selectedsBtn;
}
-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame])
    {
        [self setUp];
      //必需设置背景颜色,否则会绘画出乱
        self.backgroundColor=[UIColor clearColor];
    }
    return self;
}
-(void)setUp
{
    //创建9个按钮
    for (int i=0; i<9; i++)
    {
        UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
        //不允许用户交互,按钮就不能点击,也就不能达到高亮状态
        btn.userInteractionEnabled=NO;
        btn.tag = i;
        [btn setImage:[UIImage imageNamed:@"gesture_node_normal"] forState:UIControlStateNormal];
        [btn setImage:[UIImage imageNamed:@"gesture_node_highlighted"] forState:UIControlStateSelected];
        [self addSubview:btn];
        
    }
    UIPanGestureRecognizer *pan=[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(pan:)];
    [self addGestureRecognizer:pan];
}
//只要调用这个方法,就会把之前绘制的东西全部清掉,重新绘制
- (void)drawRect:(CGRect)rect
{
    //没有选中按钮,不需要连线
    if (self.selectedsBtn.count ==0) return;
    //把所有选中按钮中心点连线
    UIBezierPath *path = [UIBezierPath bezierPath];
    
    NSUInteger count = self.selectedsBtn.count;
    //把所有选中按钮之间都连好线
    for (int i = 0; i0)
    {
        [path addLineToPoint:_curtentP];
    }
    
    [[UIColor greenColor] set];
    path.lineWidth = 10;
    path.lineJoinStyle = kCGLineJoinRound;
    [path stroke];
    
}
-(void)pan:(UIPanGestureRecognizer*)pan
{
    //获取触摸点
    _curtentP = [pan locationInView:self];
    //判断触摸点在不在按钮上
    for (UIButton*btn in self.subviews)
    {
        //点在不在某个范围内,并且按钮没有被选中
        //CGRectContainsPoint(btn.frame, _curtentP) 判断这个点是不是在这个范围同一个坐标系
        if (CGRectContainsPoint(btn.frame, _curtentP) && btn.selected==NO)
        {
            //点在按钮上
            btn.selected = YES;
            //保存到数组中
            [self.selectedsBtn addObject:btn];
        }
    }
    
    if (pan.state == UIGestureRecognizerStateEnded)
    {
        //创建可变字符串
        NSMutableString *strM = [NSMutableString string];
        //保存输入密码
        for (UIButton*btn in self.selectedsBtn)
        {
            [strM appendFormat:@"%ld",btn.tag];
            btn.selected=NO;
            
        }
        NSLog(@"%@",strM);
        
        //还原界面
        //清除画线,把选中按钮清空
        [self.selectedsBtn removeAllObjects];
        
    }
   
    //重绘
    [self setNeedsDisplay];
    
}
//为什么要在这个方法布局子控件,因为只要一调用这个方法,就表示父控件的尺寸确定
-(void)layoutSubviews
{
    [super layoutSubviews];
    
    NSUInteger count = self.subviews.count;
    int cols = 3;
    CGFloat x = 0;
    CGFloat y = 0;
    CGFloat w = 74;
    CGFloat h = 74;
    
    CGFloat margin = (self.bounds.size.width - cols*w)/(cols+1);
    
    CGFloat col = 0;
    CGFloat row = 0;
    for (NSUInteger i=0; i

你可能感兴趣的:(手势解锁)