iOS 手势解锁

#import "LockView.h"


@interface LockView ()

@property(nonatomic, strong) NSMutableArray * lockSelectedArr;
@property(nonatomic, assign) CGPoint curPoint;

@end


@implementation LockView

-(NSMutableArray *)lockSelectedArr{
    if (_lockSelectedArr==nil) {
        _lockSelectedArr = [NSMutableArray array];
    }
    return _lockSelectedArr;
}

-(void)awakeFromNib{
    [self setUp];
}

-(void)setUp{
    for(int i=0; i<9; i++){
        UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn setImage:[UIImage imageNamed:@"gesture_node_normal"] forState:UIControlStateNormal];
        [btn setImage:[UIImage imageNamed:@"gesture_node_selected"] forState:UIControlStateSelected];
        btn.userInteractionEnabled = NO; //去除点击高亮效果
        btn.tag = i;
        [self addSubview:btn];
    }
}

-(void)layoutSubviews{
    [super layoutSubviews];
    
    CGFloat width = self.frame.size.width;
    CGFloat lockW = 74;
    CGFloat margin = (width - lockW*3)/4.0;
    
    for (int i=0; i *)touches withEvent:(UIEvent *)event{
    UITouch * touch = [touches anyObject];
    CGPoint point = [touch locationInView:self];
    UIButton * btn = [self btnRectContainsPoint:point];
    if (btn && !btn.selected) {
        btn.selected = YES;
        [self.lockSelectedArr addObject:btn];
    }
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    UITouch * touch = [touches anyObject];
    CGPoint point = [touch locationInView:self];
    self.curPoint = point;
    UIButton * btn = [self btnRectContainsPoint:point];
    if (btn && !btn.selected) {
        if([self isAdjacentBtn1:btn andBtn2:[self.lockSelectedArr lastObject]]){
            btn.selected = YES;
            [self.lockSelectedArr addObject:btn];
        }
    }
    [self setNeedsDisplay];
}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    NSMutableString * keys = [NSMutableString string];
    for (UIButton *btn in self.lockSelectedArr) {
        btn.selected = NO;
        [keys appendFormat:@"-%lu",btn.tag];
    }
    [keys deleteCharactersInRange:NSMakeRange(0, 1)];
    NSLog(@"%@", keys);
    
    [self.lockSelectedArr removeAllObjects];
    [self setNeedsDisplay];
}



- (void)drawRect:(CGRect)rect {
    if (self.lockSelectedArr.count) {
        UIBezierPath * path = [UIBezierPath bezierPath];
        
        for (int i=0; i

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