分享一个类似QQ屏保解锁的代码类

2024-06-23 10:58

本文主要是介绍分享一个类似QQ屏保解锁的代码类,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

话不多说了,直接上代码:
.h文件
//
//  GestureCodeViewController.h
//  DailyRecord
//
//  Created by sven on 13-6-20.
//  Copyright (c) 2013年 sven. All rights reserved.
//#import <UIKit/UIKit.h>#define LOCK_POINT_TAG      1000 
@interface GestureCodeViewController : UIViewController
{UIImageView *m_imageView;CGPoint m_lineStartPoint;CGPoint m_lineEndPoint;NSMutableArray *m_mutArrButtons;NSMutableArray *m_mutArrSelectedButtons;BOOL m_drawFlag;UIImage *m_pointImage;UIImage *m_selectedImage;
}@property (retain, nonatomic) UIImageView *m_imageView;@property (assign, nonatomic) CGPoint m_lineStartPoint;
@property (assign, nonatomic) CGPoint m_lineEndPoint;@property (retain, nonatomic) NSMutableArray *m_mutArrButtons;
@property (retain, nonatomic) NSMutableArray *m_mutArrSelectedButtons;@property (assign, nonatomic) BOOL m_drawFlag;@property (retain, nonatomic) UIImage *m_pointImage;
@property (retain, nonatomic) UIImage *m_selectedImage;@end
.m文件
//
//  GestureCodeViewController.m
//  DailyRecord
//
//  Created by sven on 13-6-20.
//  Copyright (c) 2013年 sven. All rights reserved.
//#import "GestureCodeViewController.h"@interface GestureCodeViewController ()@end@implementation GestureCodeViewController@synthesize m_imageView;
@synthesize m_lineStartPoint;
@synthesize m_lineEndPoint;
@synthesize m_drawFlag;
@synthesize m_mutArrButtons;
@synthesize m_mutArrSelectedButtons;
@synthesize m_pointImage;
@synthesize m_selectedImage;#pragma mark -life cycle- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];if (self) {// Custom initialization}return self;
}- (void)viewDidLoad
{[super viewDidLoad];// Do any additional setup after loading the view, typically from a nib.UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.view.bounds];imageView.backgroundColor = [UIColor whiteColor];self.m_imageView = imageView;[imageView release];[self.view addSubview:m_imageView];[self createLockPoints];
}- (void)didReceiveMemoryWarning
{[super didReceiveMemoryWarning];// Dispose of any resources that can be recreated.
}- (void)dealloc
{[m_imageView release], m_imageView = nil;[m_mutArrButtons release], m_mutArrButtons = nil;[m_mutArrSelectedButtons release], m_mutArrSelectedButtons = nil;[m_pointImage release], m_pointImage = nil;[m_selectedImage release], m_selectedImage = nil;[super dealloc];
}#pragma mark - Trace Touch Point- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{UITouch *touch = [touches anyObject];if (touch){for (UIButton *btn in self.m_mutArrButtons){CGPoint touchPoint = [touch locationInView:btn];if ([btn pointInside:touchPoint withEvent:nil]){self.m_lineStartPoint = btn.center;self.m_drawFlag = YES;if (!self.m_mutArrSelectedButtons){self.m_mutArrSelectedButtons = [NSMutableArray arrayWithCapacity:9];}[self.m_mutArrSelectedButtons addObject:btn];[btn setImage:self.m_selectedImage forState:UIControlStateNormal];}}}
}- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{UITouch *touch = [touches anyObject];if (touch && self.m_drawFlag){self.m_lineEndPoint = [touch locationInView:self.m_imageView];for (UIButton *btn in self.m_mutArrButtons){CGPoint touchPoint = [touch locationInView:btn];if ([btn pointInside:touchPoint withEvent:nil]){BOOL btnContained = NO;for (UIButton *selectedBtn in self.m_mutArrSelectedButtons){if (btn == selectedBtn){btnContained = YES;break;}}if (!btnContained){[self.m_mutArrSelectedButtons addObject:btn];[btn setImage:self.m_selectedImage forState:UIControlStateNormal];}}}self.m_imageView.image = [self drawUnlockLine];}
}- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{[self outputSelectedButtons];self.m_drawFlag = NO;self.m_imageView.image = nil;self.m_mutArrSelectedButtons = nil;
}- (void)createLockPoints
{self.m_pointImage = [UIImage imageNamed:@"blue_circle"];self.m_selectedImage = [UIImage imageNamed:@"yellow_circle"];float marginTop = 150;float marginLeft = 40;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.m_pointImage forState:UIControlStateNormal];[btn setImage:self.m_selectedImage forState:UIControlStateHighlighted];btn.frame = (CGRect){x+marginLeft, y+marginTop, self.m_pointImage.size};[self.m_imageView addSubview:btn];btn.userInteractionEnabled = NO;btn.tag = LOCK_POINT_TAG + i * 3 + j;if (!self.m_mutArrButtons) {self.m_mutArrButtons = [NSMutableArray arrayWithCapacity:9];}[self.m_mutArrButtons addObject:btn];}}
}#pragma mark - Draw Line- (UIImage *)drawUnlockLine
{UIImage *image = nil;UIColor *color = [UIColor yellowColor];CGFloat width = 5.0f;CGSize imageContextSize = self.m_imageView.frame.size;UIGraphicsBeginImageContext(imageContextSize);CGContextRef context = UIGraphicsGetCurrentContext();CGContextSetLineWidth(context, width);CGContextSetStrokeColorWithColor(context, [color CGColor]);CGContextMoveToPoint(context, self.m_lineStartPoint.x, self.m_lineStartPoint.y);for (UIButton *selectedBtn in self.m_mutArrSelectedButtons){CGPoint btnCenter = selectedBtn.center;CGContextAddLineToPoint(context, btnCenter.x, btnCenter.y);CGContextMoveToPoint(context, btnCenter.x, btnCenter.y);}CGContextAddLineToPoint(context, self.m_lineEndPoint.x, self.m_lineEndPoint.y);CGContextStrokePath(context);image = UIGraphicsGetImageFromCurrentImageContext();UIGraphicsEndImageContext();return image;
}#pragma mark -- (void)outputSelectedButtons
{for (UIButton *btn in self.m_mutArrSelectedButtons){[btn setImage:self.m_pointImage forState:UIControlStateNormal];NSLog(@"Selected-button's tag : %d\n", btn.tag);}}@end

    是不是很好用呢!

这篇关于分享一个类似QQ屏保解锁的代码类的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1086962

相关文章

Python中你不知道的gzip高级用法分享

《Python中你不知道的gzip高级用法分享》在当今大数据时代,数据存储和传输成本已成为每个开发者必须考虑的问题,Python内置的gzip模块提供了一种简单高效的解决方案,下面小编就来和大家详细讲... 目录前言:为什么数据压缩如此重要1. gzip 模块基础介绍2. 基本压缩与解压缩操作2.1 压缩文

Java中调用数据库存储过程的示例代码

《Java中调用数据库存储过程的示例代码》本文介绍Java通过JDBC调用数据库存储过程的方法,涵盖参数类型、执行步骤及数据库差异,需注意异常处理与资源管理,以优化性能并实现复杂业务逻辑,感兴趣的朋友... 目录一、存储过程概述二、Java调用存储过程的基本javascript步骤三、Java调用存储过程示

Visual Studio 2022 编译C++20代码的图文步骤

《VisualStudio2022编译C++20代码的图文步骤》在VisualStudio中启用C++20import功能,需设置语言标准为ISOC++20,开启扫描源查找模块依赖及实验性标... 默认创建Visual Studio桌面控制台项目代码包含C++20的import方法。右键项目的属性:

MySQL数据库的内嵌函数和联合查询实例代码

《MySQL数据库的内嵌函数和联合查询实例代码》联合查询是一种将多个查询结果组合在一起的方法,通常使用UNION、UNIONALL、INTERSECT和EXCEPT关键字,下面:本文主要介绍MyS... 目录一.数据库的内嵌函数1.1聚合函数COUNT([DISTINCT] expr)SUM([DISTIN

Java实现自定义table宽高的示例代码

《Java实现自定义table宽高的示例代码》在桌面应用、管理系统乃至报表工具中,表格(JTable)作为最常用的数据展示组件,不仅承载对数据的增删改查,还需要配合布局与视觉需求,而JavaSwing... 目录一、项目背景详细介绍二、项目需求详细介绍三、相关技术详细介绍四、实现思路详细介绍五、完整实现代码

Go语言代码格式化的技巧分享

《Go语言代码格式化的技巧分享》在Go语言的开发过程中,代码格式化是一个看似细微却至关重要的环节,良好的代码格式化不仅能提升代码的可读性,还能促进团队协作,减少因代码风格差异引发的问题,Go在代码格式... 目录一、Go 语言代码格式化的重要性二、Go 语言代码格式化工具:gofmt 与 go fmt(一)

Java Web实现类似Excel表格锁定功能实战教程

《JavaWeb实现类似Excel表格锁定功能实战教程》本文将详细介绍通过创建特定div元素并利用CSS布局和JavaScript事件监听来实现类似Excel的锁定行和列效果的方法,感兴趣的朋友跟随... 目录1. 模拟Excel表格锁定功能2. 创建3个div元素实现表格锁定2.1 div元素布局设计2.

HTML5实现的移动端购物车自动结算功能示例代码

《HTML5实现的移动端购物车自动结算功能示例代码》本文介绍HTML5实现移动端购物车自动结算,通过WebStorage、事件监听、DOM操作等技术,确保实时更新与数据同步,优化性能及无障碍性,提升用... 目录1. 移动端购物车自动结算概述2. 数据存储与状态保存机制2.1 浏览器端的数据存储方式2.1.

基于 HTML5 Canvas 实现图片旋转与下载功能(完整代码展示)

《基于HTML5Canvas实现图片旋转与下载功能(完整代码展示)》本文将深入剖析一段基于HTML5Canvas的代码,该代码实现了图片的旋转(90度和180度)以及旋转后图片的下载... 目录一、引言二、html 结构分析三、css 样式分析四、JavaScript 功能实现一、引言在 Web 开发中,

Python如何去除图片干扰代码示例

《Python如何去除图片干扰代码示例》图片降噪是一个广泛应用于图像处理的技术,可以提高图像质量和相关应用的效果,:本文主要介绍Python如何去除图片干扰的相关资料,文中通过代码介绍的非常详细,... 目录一、噪声去除1. 高斯噪声(像素值正态分布扰动)2. 椒盐噪声(随机黑白像素点)3. 复杂噪声(如伪