#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<self.subviews.count; i++) {
UIButton * btn = self.subviews[i];
int row = i/3;
int col = i%3;
CGRect frame = CGRectMake((col+1)*margin+col*lockW, (row+1)*margin+row*lockW, lockW, lockW);
btn.frame = frame;
}
}
-(UIButton *)btnRectContainsPoint:(CGPoint)point{
for (UIButton *btn in self.subviews) {
if (CGRectContainsPoint(btn.frame, point)) {
return btn;
}
}
return nil;
}
//是否是四周相邻点,避免线交叉
-(BOOL)isAdjacentBtn1:(UIButton *)btn1 andBtn2:(UIButton *)btn2{
int row1 = (int)(btn1.tag)/3;
int col1 = btn1.tag%3;
int row2 = (int)(btn2.tag)/3;
int col2 = btn2.tag%3;
if (abs(row1-row2)<=1 && abs(col1-col2)<=1 && btn1.tag!=btn2.tag) {
return YES;
}else{
return NO;
}
}
-(void)touchesBegan:(NSSet<UITouch *> *)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<UITouch *> *)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<UITouch *> *)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<self.lockSelectedArr.count; i++) {
UIButton * btn = self.lockSelectedArr[i];
if (i==0) {
[path moveToPoint:btn.center];
}else{
[path addLineToPoint:btn.center];
}
}
[path addLineToPoint:self.curPoint];
[path setLineWidth:10];
[path setLineJoinStyle:kCGLineJoinRound];
[[UIColor redColor] set];
[path stroke];
}
}
@end
iOS 手势解锁
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- Demo地址 1.手势密码 API 使用方法 2.指纹验证 关于指纹验证,之前一篇文章已经写过,这里简单封装了下 ...