Add gesture lock screen, unlock function on iOS

Source: Internet
Author: User

Add gesture lock screen, unlock function on iOS

In some cases involving personal privacy, especially when the mobile device contains too much private information, it is necessary for the user's security considerations.

The desktop version of QQ in many years ago to consider the user left the computer after the privacy of the risk of disclosure, provided "leave the computer auto-lock" or "idle lock" and other similar functions, I also forgot.

And on the iphone version of QQ, also provides the function of gesture lock. Such as:


In my last post, I briefly mentioned how to move a line based on my finger, and here is a further version, still just a rough prototype:

The specific code is implemented as follows:


[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];
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 the 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

Add gesture lock screen, unlock function on iOS

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.