- 在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.在不关闭系统软件的情况下节省电量 对于市面上已有的锁屏 ...
随机推荐
-
Tomcat 常用配置及网站部署
一.同一Tomcat 多个端口部署不同的项目 在tomcat 安装目录下C:/Program Files/apache-tomcat-6.0.29/conf找到server.xml (1 ...
-
hybird混合式开发搭建
1.xml <?xml version="1.0" encoding="utf-8"?> <WebView xmlns:android=&qu ...
-
理论沉淀:RANSAC算法
1.解决问题: 当一组样本数据中含有(较小波动的)正常数据(inliers)和(较大波动的)异常数据(outliers)且异常数据的量还不小于正常数据的量时,用最小二乘法将难以获得期望的直线(即能拟合 ...
-
关于C++需要加强学习的几点内容
1.C++ effective指导原则 2.C++标准库 3.数据结构算法 4.C++设计模式 5.shell脚本 6.python
-
Asp.net GridView 72般绝技
快速预览:GridView无代码分页排序GridView选中,编辑,取消,删除GridView正反双向排序GridView和下拉菜单DropDownList结合GridView和CheckBox结合鼠 ...
-
SwiftyUserDefaults-封装系统本地化的框架
// // ViewController.swift // Test4SwiftyUserDefaults // // Created by 马玉龙 on 2017/1/14. // Copyrigh ...
-
201521123082 《Java程序设计》第5周学习总结
201521123082 <Java程序设计>第5周学习总结 标签(空格分隔): java 1. 本周学习总结 1.1 尝试使用思维导图总结有关多态与接口的知识点. 1.2 可选:使用常规 ...
-
ArcGIS API for JavaScript 4.2学习笔记[12] View的弹窗(Popup)
看本文前最好对第二章(Mapping and Views)中的Map和View类有理解. 视图类有一个属性是Popup类型的popup,查阅API知道这个就是视图的弹窗,每一个View的实例都有一个p ...
-
学习gstreamer
1. 对gst 的框架认识. 第一篇文章有结构图说明,清楚易懂:第二篇文章介绍了gst的简单使用 http://www.cnblogs.com/jingzhishen/p/3709639.html h ...
-
三、TortoiseGit之配置密钥
TortoiseGit使用扩展名为ppk的密钥,而不是ssh-keygen生成的rsa密钥. 也就是说使用 ssh-keygen -t rsa -C "username@email.co ...