手势解锁

2019-04-29  本文已影响0人  奋斗吧程序员

在需要显示的控制器中写入如下代码引用

- (void)viewDidLoad {

    [super viewDidLoad];

    View*view = [[Viewalloc]initWithFrame:self.view.bounds];

    [self.viewaddSubview:view];}

//假设正确的手势是1-2-3-6-5-4-7-8-9

@interface View ()

@property (nonatomic,copy) NSMutableArray *array;//button数组

@property (nonatomic,copy) NSMutableArray *linearray;//需要划线的button数组

@property (nonatomic,assign) CGPoint lastPoint;//手指最后的位置

@property (nonatomic,copy) NSArray *gesture;//手势密码

@end

@implementation View

- (NSMutableArray *)array {

    if(!_array) {

        CGFloatspace =60;

        _array = [NSMutableArray arrayWithCapacity:9];

        CGFloatwidth =(self.frame.size.width-4*space)/3;

        for(inti=0;i<9;i++) {

            UIButton*btn = [[UIButtonalloc]initWithFrame:CGRectMake(space+(width+space)*(i%3),100+(width+space)*(i/3), width, width)];

            btn.tag= i+1;

            [btnsetImage:[UIImage imageNamed:@"normal"] forState:UIControlStateNormal];

            [btnsetImage:[UIImage imageNamed:@"select"] forState:UIControlStateSelected];

            [btnsetImage:[UIImage imageNamed:@"disable"] forState:UIControlStateDisabled];

            btn.userInteractionEnabled = NO;

            [selfaddSubview:btn];

            [_arrayaddObject:btn];

        }

    }

    return _array;

}

- (NSMutableArray*)linearray {

    if (!_linearray) {

        _linearray = [NSMutableArray arrayWithCapacity:9];

    }

    return _linearray;

}

- (instancetype)initWithFrame:(CGRect)frame {

    if(self= [superinitWithFrame:frame]) {

        [selfarray];

        self.gesture =[@"1,2,3,6,5,4,7,8,9" componentsSeparatedByString:@","];

        self.backgroundColor = [UIColor clearColor];

    }

    return self;

}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent*)event {

    UITouch*touch = touches.anyObject;

    CGPointpoint = [touchlocationInView:touch.view];

    for(UIButton*btninself.array) {

        if(CGRectContainsPoint(btn.frame, point)) {

            btn.selected=YES;

        }

    }

}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent*)event {

    //判断手势密码是否正确

    NSMutableArray *arr = [NSMutableArray arrayWithCapacity:9];

    for(UIButton*btninself.linearray) {

        [arraddObject:[NSStringstringWithFormat:@"%ld",(long)btn.tag]];

    }

    if(![arrisEqualToArray:self.gesture]) {

        self.lastPoint= [[self.linearraylastObject]center];

        [self setNeedsDisplay];

        for(UIButton*btninself.linearray) {

            btn.selected=NO;

            btn.enabled=NO;

        }

        self.userInteractionEnabled = NO;

        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5*NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

            self.userInteractionEnabled = YES;

            [selftouchEnd];

            NSLog(@"手势错误!!!");

        });

    }else{

        [selftouchEnd];

        NSLog(@"手势正确!!!");

    }

}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent*)event {

    UITouch*touch = touches.anyObject;

    CGPointpoint = [touchlocationInView:touch.view];

    for(UIButton*btninself.array) {

        if(CGRectContainsPoint(btn.frame, point)) {

            btn.selected=YES;

            if(![self.linearraycontainsObject:btn]) {

                [self.linearrayaddObject:btn];

            }

        }

    }

    self.lastPoint= point;

    [self setNeedsDisplay];

}

- (void)touchEnd {

    for(UIButton*btninself.array) {

        btn.selected=NO;

        btn.enabled=YES;

    }

    [self.linearray removeAllObjects];

    [self setNeedsDisplay];

}

- (void)drawRect:(CGRect)rect {

    if(self.linearray.count==0) {

        return;

    }

    UIBezierPath *path = [UIBezierPath bezierPath];

    for(inti=0; i

        UIButton*btn =self.linearray[i];

        if(i==0) {

            [pathmoveToPoint:btn.center];

        }else{

            [pathaddLineToPoint:btn.center];

        }

    }

    [pathaddLineToPoint:self.lastPoint];

    path.lineWidth=5;

    path.lineCapStyle = kCGLineCapRound;

    path.lineJoinStyle = kCGLineJoinRound;

    [[UIColor blueColor] set];

    [pathstroke];

}

@end

上一篇 下一篇

猜你喜欢

热点阅读