在iOS上增加手势锁屏、解锁功能
- 在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上增加手势锁屏、解锁功能的更多相关文章
- iOS 音乐播放器之锁屏效果+歌词解析
概述 功能描述:锁屏歌曲信息.控制台远程控制音乐播放:暂停/播放.上一首/下一首.快进/快退.列表菜单弹框和拖拽控制台的进度条调节进度(结合了QQ音乐和网易云音乐在锁屏状态下的效果).歌词解析并随音乐 ...
- 防止 IOS 和 安卓 自动锁屏
Ios代码 在文件AppController中的 didFinishLaunchingWithOptions函数中加一行代码即可: [[UIApplication sharedApplication] ...
- Windows10 上的国产锁屏广告?
不知从什么时候开始,我的笔记本(Windows 10 Home,联想X1)在开机.锁屏时都会显示一些国产的“公益广告”(可惜不能截屏),有时是关于时令节气,有时是一些鸡汤短句,有时节假日则是叫我爱党爱 ...
- iOS - Quartz 2D 手势截屏绘制
1.绘制手势截屏 具体实现代码见 GitHub 源码 QExtension QTouchClipView.h @interface QTouchClipView : UIView /** * 创建手势 ...
- iOS 不让自动锁屏
[UIApplication sharedApplication].idleTimerDisabled=YES;
- 【腾讯Bugly干货分享】浅谈Android自定义锁屏页的发车姿势
本文来自于腾讯bugly开发者社区,非经作者同意,请勿转载,原文地址:http://dev.qq.com/topic/57875330c9da73584b025873 一.为什么需要自定义锁屏页 锁屏 ...
- 浅谈 Android 自定义锁屏页的发车姿势
作者:blowUp,原文链接:http://mp.weixin.qq.com/s?__biz=MzA3NTYzODYzMg==&mid=2653577446&idx=2&sn= ...
- 浅谈Android自定义锁屏页的发车姿势
一.为什么需要自定义锁屏页 锁屏作为一种黑白屏时代就存在的手机功能,至今仍发挥着巨大作用,特别是触屏时代的到来,锁屏的功用被发挥到了极致.多少人曾经在无聊的时候每隔几分钟划开锁屏再关上,孜孜不倦,其酸 ...
- 自选项目--手机锁屏软件--NABC分析
N(Need 需求) 关键字:利用碎片时间加强对想记的事物的记忆.备忘.一般来说,锁屏目的大致有三点: 1.保护手机隐私 2.防止误操作手机 3.在不关闭系统软件的情况下节省电量 对于市面上已有的锁屏 ...
随机推荐
- Matlab中取模(mod)与取余(rem)的区别
取模(mod)与取余(rem)是不同的,通常取模运算也叫取余运算,它们返回结果都是余数. rem和mod唯一的区别在于: 当x和y的正负号一样的时候,两个函数结果是等同的:当x和y的符号不同时,rem ...
- 【转】在SQL Server 2008中SA密码丢失了怎么办?
sql server 2008的sa用户莫名其妙就登陆不进去了.提示如下: 以上提示就表明是密码错误,但密码我可是记得牢牢的,也许是系统被黑的原因吧.一直以来我的Windows身份验证就用不起,以下方 ...
- Excel表格中汉字转拼音
一.使用“实用汉字转拼音V4.8” 软件 下载地址http://www.orsoon.com/soft/4413.html 或则百度 很多的 二.Excel自定义函数方法: 1.启动Excel 200 ...
- google base之LockImpl
为了兼容不同的平台,这个类采用了impl模式,win平台通过CRITICAL_SECTION, 这样的话还是相对比较简单,具体就不详解了,不过不得不说boost的实现方式就要复杂到哪里去了,当然,好处 ...
- 建立一个ROS msg and srv
msg是一个描述ROS消息字段的简单的文本文件,它们经常用来为消息产生不同语言的源代码. srv文件描述一个服务,它由请求和响应两部分组成. msg文件被存储在一个包的msg目录下,srv文件被存储在 ...
- hdu 4455 Substrings(找规律&DP)
Substrings Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- MySQLD 配置
http://blog.163.com/sir_876/blog/static/11705223201372710303382/ http://www.kankanews.com/ICkengine/ ...
- QT5.4 计算器程序 打包&发布,解决dll的最新解决方案(图文并茂,很清楚)
QT写界面还是很不错,就是打包会比较麻烦,折腾了一天总算是打包完成了. QT软件的打包发布一个难点是必备dll文件的识别,现在高版本QT自带了一个windeployqt工具,直接会把需要的dll生成一 ...
- iOS 首次启动画面,新装或更新用户可以通过它查看简介。
// // GuideViewController.h // Guide // // Created by twb on 13-9-17. // Copyright (c) 2013年 twb. Al ...
- 化简复杂逻辑,编写紧凑的if条件语句(二):依据if子句顺序化简条件
<化简复杂逻辑,编写紧凑的if条件语句>已经得出了跳.等.飞.异常的各自条件,方便起见这里重新贴一下. 立即跃迁:!a && b && d 等待跃迁:!a ...