在iOS上增加手势锁屏、解锁功能

转载自:http://www.2cto.com/kf/201209/158186.html

在一些涉及个人隐私的场景下,尤其是当移动设备包含太多私密信息时,为用户的安全考虑是有必要的。

桌面版的QQ在很多年前就考虑到用户离开电脑后隐私泄露的危险,提供了“离开电脑自动锁定”或者“闲置锁定”等类似功能,具体我也忘了。

而在iPhone版的QQ上,也提供了手势锁的功能。如下图:

 


我在上一篇博文中简单提到如何根据手指移动画线条,而这里是进一步的版本,仍然只是粗糙原型:

 

 

具体的代码实现如下:


[cpp] 
// 
//  ViewController.m 
//  GestureLock 
// 
//  Created by Jason Lee on 12-9-26. 
//  Copyright (c) 2012年 Jason Lee. All rights reserved. 
// 
 
#import "ViewController.h" 
 
#define LOCK_POINT_TAG      1000 
 
@interface ViewController () 
 
@property (nonatomic, strong) UIImageView *imageView; 
 
@property (nonatomic, assign) CGPoint lineStartPoint; 
@property (nonatomic, assign) CGPoint lineEndPoint; 
 
@property (nonatomic, strong) NSMutableArray *buttonArray; 
@property (nonatomic, strong) NSMutableArray *selectedButtons; 
 
@property (nonatomic, assign) BOOL drawFlag; 
 
@property (nonatomic, strong) UIImage *pointImage; 
@property (nonatomic, strong) UIImage *selectedImage; 
 
@end 
 
@implementation ViewController 
 
- (void)dealloc 

    [super dealloc]; 
    // 
    [_imageView release]; 
    [_buttonArray release]; 
    [_selectedButtons release]; 
    [_pointImage release]; 
    [_selectedImage release]; 

 
- (void)viewDidLoad 

    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 
     
    _imageView = [[UIImageView alloc] initWithFrame:self.view.bounds]; 
    [self.view addSubview:self.imageView]; 
    self.imageView.backgroundColor = [UIColor whiteColor]; 
     
    [self createLockPoints]; 

 
- (void)didReceiveMemoryWarning 

    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 

 
#pragma mark - Trace Touch Point 
 
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 

    UITouch *touch = [touches anyObject]; 
    if (touch) { 
        for (UIButton *btn in self.buttonArray) { 
            CGPoint touchPoint = [touch locationInView:btn]; 
            if ([btn pointInside:touchPoint withEvent:nil]) { 
                self.lineStartPoint = btn.center; 
                self.drawFlag = YES; 
                 
                if (!self.selectedButtons) { 
                    self.selectedButtons = [NSMutableArray arrayWithCapacity:9]; 
                } 
                 
                [self.selectedButtons addObject:btn]; 
                [btn setImage:self.selectedImage forState:UIControlStateNormal]; 
            } 
        } 
    } 

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

    UITouch *touch = [touches anyObject]; 
    if (touch && self.drawFlag) { 
        self.lineEndPoint = [touch locationInView:self.imageView]; 
         
        for (UIButton *btn in self.buttonArray) { 
            CGPoint touchPoint = [touch locationInView:btn]; 
             
            if ([btn pointInside:touchPoint withEvent:nil]) { 
                BOOL btnContained = NO; 
                 
                for (UIButton *selectedBtn in self.selectedButtons) { 
                    if (btn == selectedBtn) { 
                        btnContained = YES; 
                        break; 
                    } 
                } 
                 
                if (!btnContained) { 
                    [self.selectedButtons addObject:btn]; 
                    [btn setImage:self.selectedImage forState:UIControlStateNormal]; 
                } 
            } 
        } 
         
        self.imageView.image = [self drawUnlockLine]; 
    } 

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

    [self outputSelectedButtons]; 
     
    self.drawFlag = NO; 
    self.imageView.image = nil; 
    self.selectedButtons = nil; 

 
#pragma mark - Create Lock Points 
 
- (void)createLockPoints 

    self.pointImage = [UIImage imageNamed:@"blue_circle"]; 
    self.selectedImage = [UIImage imageNamed:@"yellow_circle"]; 
     
    float marginTop = 100; 
    float marginLeft = 45; 
     
    float y; 
    for (int i = 0; i < 3; ++i) { 
        y = i * 100; 
        float x; 
        for (int j = 0; j < 3; ++j) { 
            x = j * 100; 
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom]; 
            [btn setImage:self.pointImage forState:UIControlStateNormal]; 
            [btn setImage:self.selectedImage forState:UIControlStateHighlighted]; 
            btn.frame = (CGRect){x+marginLeft, y+marginTop, self.pointImage.size}; 
            [self.imageView addSubview:btn]; 
            btn.userInteractionEnabled = NO; 
            btn.tag = LOCK_POINT_TAG + i * 3 + j; 
             
            if (!self.buttonArray) { 
                self.buttonArray = [NSMutableArray arrayWithCapacity:9]; 
            } 
            [self.buttonArray addObject:btn]; 
        } 
    } 

 
#pragma mark - Draw Line 
 
- (UIImage *)drawUnlockLine 

    UIImage *image = nil; 
     
    UIColor *color = [UIColor yellowColor]; 
    CGFloat width = 5.0f; 
    CGSize imageContextSize = self.imageView.frame.size; 
     
    UIGraphicsBeginImageContext(imageContextSize); 
     
    CGContextRef context = UIGraphicsGetCurrentContext(); 
     
    CGContextSetLineWidth(context, width); 
    CGContextSetStrokeColorWithColor(context, [color CGColor]); 
     
    CGContextMoveToPoint(context, self.lineStartPoint.x, self.lineStartPoint.y); 
    for (UIButton *selectedBtn in self.selectedButtons) { 
        CGPoint btnCenter = selectedBtn.center; 
        CGContextAddLineToPoint(context, btnCenter.x, btnCenter.y); 
        CGContextMoveToPoint(context, btnCenter.x, btnCenter.y); 
    } 
    CGContextAddLineToPoint(context, self.lineEndPoint.x, self.lineEndPoint.y); 
     
    CGContextStrokePath(context); 
     
    image = UIGraphicsGetImageFromCurrentImageContext(); 
     
    UIGraphicsEndImageContext(); 
     
    return image; 

 
#pragma mark -  
 
- (void)outputSelectedButtons 

    for (UIButton *btn in self.selectedButtons) { 
        [btn setImage:self.pointImage forState:UIControlStateNormal]; 
        NSLog(@"Selected-button's tag : %d\n", btn.tag); 
    } 

 
@end 

Jason Lee @ Hangzhou



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
资源包主要包含以下内容: ASP项目源码:每个资源包中都包含完整的ASP项目源码,这些源码采用了经典的ASP技术开发,结构清晰、注释详细,帮助用户轻松理解整个项目的逻辑和实现方式。通过这些源码,用户可以学习到ASP的基本语法、服务器端脚本编写方法、数据库操作、用户权限管理等关键技术。 数据库设计文件:为了方便用户更好地理解系统的后台逻辑,每个项目中都附带了完整的数据库设计文件。这些文件通常包括数据库结构图、数据表设计文档,以及示例数据SQL脚本。用户可以通过这些文件快速搭建项目所需的数据库环境,并了解各个数据表之间的关系和作用。 详细的开发文档:每个资源包都附有详细的开发文档,文档内容包括项目背景介绍、功能模块说明、系统流程图、用户界面设计以及关键代码解析等。这些文档为用户提供了深入的学习材料,使得即便是从零开始的开发者也能逐步掌握项目开发的全过程。 项目演示与使用指南:为帮助用户更好地理解和使用这些ASP项目,每个资源包中都包含项目的演示文件和使用指南。演示文件通常以视频或图文形式展示项目的主要功能和操作流程,使用指南则详细说明了如何配置开发环境、部署项目以及常见问题的解决方法。 毕业设计参考:对于正在准备毕业设计的学生来说,这些资源包是绝佳的参考材料。每个项目不仅功能完善、结构清晰,还符合常见的毕业设计要求和标准。通过这些项目,学生可以学习到如何从零开始构建一个完整的Web系统,并积累丰富的项目经验。
好的,下面是手势解锁的Demo实现过程: 1. 首先创建一个UIView的子类,作为手势解锁的主体视图,我们称之为`GestureLockView`。 2. 在`GestureLockView`中创建一个数组`circleArray`,用于存储手势解锁的圆点。 ``` @property (nonatomic, strong) NSMutableArray *circleArray; ``` 3. 在`GestureLockView`的`layoutSubviews`方法中,创建9个圆点,并加入到`circleArray`中。 ``` - (void)layoutSubviews { [super layoutSubviews]; CGFloat margin = (self.frame.size.width - 3 * kCircleSize) / 4.0; for (int i = 0; i < 9; i++) { CGFloat x = margin + (i % 3) * (margin + kCircleSize); CGFloat y = margin + (i / 3) * (margin + kCircleSize); CGRect frame = CGRectMake(x, y, kCircleSize, kCircleSize); GestureLockCircle *circle = [[GestureLockCircle alloc] initWithFrame:frame]; circle.tag = i + 1; [self addSubview:circle]; [self.circleArray addObject:circle]; } } ``` 4. 在`GestureLockView`中创建一个数组`selectedArray`,用于存储用户选择的圆点。 ``` @property (nonatomic, strong) NSMutableArray *selectedArray; ``` 5. 在`GestureLockView`中实现手势识别的方法`touchesMoved:withEvent:`,通过判断触摸点是否在圆点内来确定用户选择的圆点,并绘制用户选择的线条。 ``` - (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { UITouch *touch = [touches anyObject]; CGPoint point = [touch locationInView:self]; for (GestureLockCircle *circle in self.circleArray) { if (CGRectContainsPoint(circle.frame, point) && !circle.selected) { circle.selected = YES; [self.selectedArray addObject:circle]; break; } } self.currentPoint = point; [self setNeedsDisplay]; } ``` 6. 在`GestureLockView`中实现绘制方法`drawRect:`,根据用户选择的圆点绘制线条。 ``` - (void)drawRect:(CGRect)rect { if (self.selectedArray.count == 0) { return; } UIBezierPath *path = [UIBezierPath bezierPath]; path.lineWidth = kLineWidth; path.lineJoinStyle = kCGLineJoinRound; path.lineCapStyle = kCGLineCapRound; [[UIColor whiteColor] set]; for (int i = 0; i < self.selectedArray.count; i++) { GestureLockCircle *circle = self.selectedArray[i]; if (i == 0) { [path moveToPoint:circle.center]; } else { [path addLineToPoint:circle.center]; } } [path addLineToPoint:self.currentPoint]; [path stroke]; } ``` 7. 在`GestureLockView`中实现手势结束的方法`touchesEnded:withEvent:`,判断用户手势是否正确,并通过代理方法通知外部。 ``` - (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { NSMutableString *password = [NSMutableString string]; for (GestureLockCircle *circle in self.selectedArray) { [password appendFormat:@"%ld", circle.tag]; } BOOL success = [password isEqualToString:self.password]; if (success) { for (GestureLockCircle *circle in self.selectedArray) { circle.selected = NO; } [self.selectedArray removeAllObjects]; [self setNeedsDisplay]; if (self.delegate && [self.delegate respondsToSelector:@selector(gestureLockView:didCompleteWithPassword:)]) { [self.delegate gestureLockView:self didCompleteWithPassword:password]; } } else { for (GestureLockCircle *circle in self.selectedArray) { circle.selected = NO; circle.error = YES; } [self.selectedArray removeAllObjects]; [self setNeedsDisplay]; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(kErrorDuration * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ for (GestureLockCircle *circle in self.circleArray) { circle.error = NO; } [self setNeedsDisplay]; }); } } ``` 8. 在外部创建`GestureLockView`实例,并设置代理方法,实现手势解锁的逻辑。 ``` - (void)viewDidLoad { [super viewDidLoad]; GestureLockView *lockView = [[GestureLockView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenWidth)]; lockView.center = self.view.center; lockView.delegate = self; lockView.password = @"123456789"; [self.view addSubview:lockView]; } #pragma mark - GestureLockViewDelegate - (void)gestureLockView:(GestureLockView *)lockView didCompleteWithPassword:(NSString *)password { NSLog(@"password: %@", password); } ``` 至此,手势解锁的Demo已经完成了,你可以尝试在模拟器或真机上运行它。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值