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

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

桌面版的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

 

在iOS上增加手势锁屏、解锁功能的更多相关文章

  1. iOS 音乐播放器之锁屏效果+歌词解析

    概述 功能描述:锁屏歌曲信息.控制台远程控制音乐播放:暂停/播放.上一首/下一首.快进/快退.列表菜单弹框和拖拽控制台的进度条调节进度(结合了QQ音乐和网易云音乐在锁屏状态下的效果).歌词解析并随音乐 ...

  2. 防止 IOS 和 安卓 自动锁屏

    Ios代码 在文件AppController中的 didFinishLaunchingWithOptions函数中加一行代码即可: [[UIApplication sharedApplication] ...

  3. Windows10 上的国产锁屏广告?

    不知从什么时候开始,我的笔记本(Windows 10 Home,联想X1)在开机.锁屏时都会显示一些国产的“公益广告”(可惜不能截屏),有时是关于时令节气,有时是一些鸡汤短句,有时节假日则是叫我爱党爱 ...

  4. iOS - Quartz 2D 手势截屏绘制

    1.绘制手势截屏 具体实现代码见 GitHub 源码 QExtension QTouchClipView.h @interface QTouchClipView : UIView /** * 创建手势 ...

  5. iOS 不让自动锁屏

    [UIApplication sharedApplication].idleTimerDisabled=YES;

  6. 【腾讯Bugly干货分享】浅谈Android自定义锁屏页的发车姿势

    本文来自于腾讯bugly开发者社区,非经作者同意,请勿转载,原文地址:http://dev.qq.com/topic/57875330c9da73584b025873 一.为什么需要自定义锁屏页 锁屏 ...

  7. 浅谈 Android 自定义锁屏页的发车姿势

    作者:blowUp,原文链接:http://mp.weixin.qq.com/s?__biz=MzA3NTYzODYzMg==&mid=2653577446&idx=2&sn= ...

  8. 浅谈Android自定义锁屏页的发车姿势

    一.为什么需要自定义锁屏页 锁屏作为一种黑白屏时代就存在的手机功能,至今仍发挥着巨大作用,特别是触屏时代的到来,锁屏的功用被发挥到了极致.多少人曾经在无聊的时候每隔几分钟划开锁屏再关上,孜孜不倦,其酸 ...

  9. 自选项目--手机锁屏软件--NABC分析

    N(Need 需求) 关键字:利用碎片时间加强对想记的事物的记忆.备忘.一般来说,锁屏目的大致有三点: 1.保护手机隐私 2.防止误操作手机 3.在不关闭系统软件的情况下节省电量 对于市面上已有的锁屏 ...

随机推荐

  1. 指针和Const限定符

    指针和Const限定符 1.指向const对象的指针 如果指针指向的是const对象,则不允许使用指针来改变其所指的const值.C++要求指向const对象的指针具有const特性. const d ...

  2. 利用Socket实现的两个程序的通信

    写的也很简单,自己觉得挺有意思了 程序如图 主要代码 public class Message { Form1 mainfrom = null; public Message() { } public ...

  3. FINDPEAKS - matlab函数

    FINDPEAKS Find local peaks in data PKS = FINDPEAKS(X) finds local peaks in the data vector X. A loca ...

  4. 获取文件数据流+叠加byte数组(给byte数组加包头包尾)

    OpenFileDialog ofd = new OpenFileDialog(); ofd.Filter = "(*.mp4)|*.mp4|(*.*)|*.*"; ofd.Res ...

  5. nginx proxy_pass 后面的斜杠

    # add / location /app/ { proxy_pass http://$backend/; } # location /app/ { proxy_pass http://$backen ...

  6. 排序算法——交换排序(冒泡排序、快速排序)(java)

    一.冒泡排序 时间复杂度:O(n^2) 公认最慢的排序,每次把最大/最小的放一边,原理: [57,68,59,52] [57,68,59,52] [57,59,68,52] [57,59,52,68] ...

  7. JS笔记 入门第四

    小测试: 注意:取消所有的设定可以直接使用 document.getElementById("txt").removeAttribute("style"); 这 ...

  8. 重拾javascript动态客户端网页脚本

    笔记一: 一.DOM 作用: ·              DOM(Doument Object Model) 1.document文档 HTML 文件 (标记语言) <html> < ...

  9. Nancy之实现API

    Nancy之实现API的功能 0x01.前言 现阶段,用来实现API的可能大部分用的是ASP.NET Web API或者是ASP.NET MVC,毕竟是微软官方出产的,用的人也多. 但是呢,Nancy ...

  10. Umbraco学习2------数据类型

    一.基础概念 在使用Umbraco这类CMS制作网站之前,先要搞清楚的是,和概念中网站制作的区别. 暂时忘掉所谓的ADO.NET存储.忘掉ASP.NET.忘掉多层架构什么的. 只需要关注:要显示什么. ...