指纹解锁:
- (void)evaluatePolicy
{
LAContext *context = [[LAContext alloc] init];
//If set to empty string, the button will be hidden.
context.localizedFallbackTitle = @"";
// show the authentication UI with our reason string
[context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:NSLocalizedString(@"通过验证指纹解锁", nil) reply:
^(BOOL success, NSError *authenticationError) {
if (success) {
//指纹验证成功后,跳转到主界面
YDUnlockViewController *unlockVc = [[YDUnlockViewController alloc] init];
[self presentViewController:unlockVc animated:YES completion:nil];
} else {
//指纹验证失败,跳转到手势解锁界面
YDLockViewController *lockVc = [[YDLockViewController alloc] init];
[self presentViewController:lockVc animated:YES completion:nil];
}
}];
}
添加手势解锁按钮
-(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_highlighted"] forState:UIControlStateSelected];
btn.tag = i;
//不许用户点击,只能通过手指滑动监听
btn.userInteractionEnabled = NO;
[self addSubview:btn];
}
}
设置按钮的位置
-(void)layoutSubviews{
[super layoutSubviews];
CGFloat btnW = 74;
CGFloat btnH = 74;
CGFloat margin = (self.frame.size.width - (3 * btnW)) / 4;
CGFloat btnX;
CGFloat btnY;
for (int i = 0; i < self.subviews.count; i++) {
UIButton *btn = self.subviews[i];
btnX = (i % 3) * (btnW + margin) + margin;
btnY = (i / 3) * (btnH + margin) + margin;
btn.frame = CGRectMake(btnX, btnY, btnW, btnH);
}
}
获取用户刚按下时的触摸点
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
//获取按下的点
CGPoint startPoint = [self getCurrentTouchPoint:touches];
//判断触摸的位置是否在按钮的范围内
UIButton *btn = [self getMoveButton:startPoint];
if (btn && btn.selected != YES) {
btn.selected = YES;
[self.buttons addObject:btn];
}
}
获取用户手指移动的轨迹
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
//获取按下的点
CGPoint movePoint = [self getCurrentTouchPoint:touches];
//判断触摸的位置是否在按钮的范围内
UIButton *btn = [self getMoveButton:movePoint];
if (btn && btn.selected != YES) {
btn.selected = YES;
[self.buttons addObject:btn];
}
self.currentPoint = movePoint;
[self setNeedsDisplay];
}
用户手势密码输入完毕后
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
//取出用户输入的密码
NSMutableString *result = [NSMutableString string];
for (UIButton *btn in self.buttons) {
[result appendFormat:@"%d",btn.tag];
}
if ([self.delegate respondsToSelector:@selector(lockViewDidFinish:passWord:)]) {
[self.delegate lockViewDidFinish:self passWord:result];
}
//恢复按钮的状态(让数组的每个元素执行这个方法)
//不管用 不知道为什么
// [self.buttons makeObjectsPerformSelector:@selector(setSelected:) withObject:@(NO)];
for (UIButton *btn in self.buttons) {
[btn setSelected:NO];
}
//清空数组
[self.buttons removeAllObjects];
[self setNeedsDisplay];
//清空currentPoint
self.currentPoint = CGPointZero;
}
获取当前的点
-(CGPoint)getCurrentTouchPoint:(NSSet *)touches{
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView:touch.view];
return point;
}
-(UIButton *)getMoveButton:(CGPoint)point{
for (UIButton *btn in self.subviews) {
if (CGRectContainsPoint(btn.frame, point)) {
return btn;
}
}
return nil;
}
-(void)drawRect:(CGRect)rect{
//获取上下文
CGContextRef ctf = UIGraphicsGetCurrentContext();
//清空上下文
CGContextClearRect(ctf, rect);
for (int i = 0; i<self.buttons.count; i++) {
UIButton *btn = self.buttons[i];
if (0 == i) {
CGContextMoveToPoint(ctf, btn.center.x, btn.center.y);
}else{
CGContextAddLineToPoint(ctf, btn.center.x, btn.center.y);
}
}
//判断如果当前点是0,0 就不绘制
// if (!CGPointEqualToPoint(self.currentPoint, CGPointZero)) {
// //当所有按钮的中点都连接好之后,再连接手指当前的位置
// CGContextAddLineToPoint(ctf, self.currentPoint.x, self.currentPoint.y);
// }
//判断数组中是否有按钮
if (self.buttons.count != 0) {
CGContextAddLineToPoint(ctf, self.currentPoint.x, self.currentPoint.y);
}
CGContextSetLineWidth(ctf, 10);
[[UIColor redColor] set];
CGContextStrokePath(ctf);
}