分享一个类似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

相关文章

Golang操作DuckDB实战案例分享

《Golang操作DuckDB实战案例分享》DuckDB是一个嵌入式SQL数据库引擎,它与众所周知的SQLite非常相似,但它是为olap风格的工作负载设计的,DuckDB支持各种数据类型和SQL特性... 目录DuckDB的主要优点环境准备初始化表和数据查询单行或多行错误处理和事务完整代码最后总结Duck

python实现pdf转word和excel的示例代码

《python实现pdf转word和excel的示例代码》本文主要介绍了python实现pdf转word和excel的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价... 目录一、引言二、python编程1,PDF转Word2,PDF转Excel三、前端页面效果展示总结一

在MyBatis的XML映射文件中<trim>元素所有场景下的完整使用示例代码

《在MyBatis的XML映射文件中<trim>元素所有场景下的完整使用示例代码》在MyBatis的XML映射文件中,trim元素用于动态添加SQL语句的一部分,处理前缀、后缀及多余的逗号或连接符,示... 在MyBATis的XML映射文件中,<trim>元素用于动态地添加SQL语句的一部分,例如SET或W

使用C#代码计算数学表达式实例

《使用C#代码计算数学表达式实例》这段文字主要讲述了如何使用C#语言来计算数学表达式,该程序通过使用Dictionary保存变量,定义了运算符优先级,并实现了EvaluateExpression方法来... 目录C#代码计算数学表达式该方法很长,因此我将分段描述下面的代码片段显示了下一步以下代码显示该方法如

将Python应用部署到生产环境的小技巧分享

《将Python应用部署到生产环境的小技巧分享》文章主要讲述了在将Python应用程序部署到生产环境之前,需要进行的准备工作和最佳实践,包括心态调整、代码审查、测试覆盖率提升、配置文件优化、日志记录完... 目录部署前夜:从开发到生产的心理准备与检查清单环境搭建:打造稳固的应用运行平台自动化流水线:让部署像

python多进程实现数据共享的示例代码

《python多进程实现数据共享的示例代码》本文介绍了Python中多进程实现数据共享的方法,包括使用multiprocessing模块和manager模块这两种方法,具有一定的参考价值,感兴趣的可以... 目录背景进程、进程创建进程间通信 进程间共享数据共享list实践背景 安卓ui自动化框架,使用的是

SpringBoot生成和操作PDF的代码详解

《SpringBoot生成和操作PDF的代码详解》本文主要介绍了在SpringBoot项目下,通过代码和操作步骤,详细的介绍了如何操作PDF,希望可以帮助到准备通过JAVA操作PDF的你,项目框架用的... 目录本文简介PDF文件简介代码实现PDF操作基于PDF模板生成,并下载完全基于代码生成,并保存合并P

C#读取本地网络配置信息全攻略分享

《C#读取本地网络配置信息全攻略分享》在当今数字化时代,网络已深度融入我们生活与工作的方方面面,对于软件开发而言,掌握本地计算机的网络配置信息显得尤为关键,而在C#编程的世界里,我们又该如何巧妙地读取... 目录一、引言二、C# 读取本地网络配置信息的基础准备2.1 引入关键命名空间2.2 理解核心类与方法

SpringBoot基于MyBatis-Plus实现Lambda Query查询的示例代码

《SpringBoot基于MyBatis-Plus实现LambdaQuery查询的示例代码》MyBatis-Plus是MyBatis的增强工具,简化了数据库操作,并提高了开发效率,它提供了多种查询方... 目录引言基础环境配置依赖配置(Maven)application.yml 配置表结构设计demo_st

SpringCloud集成AlloyDB的示例代码

《SpringCloud集成AlloyDB的示例代码》AlloyDB是GoogleCloud提供的一种高度可扩展、强性能的关系型数据库服务,它兼容PostgreSQL,并提供了更快的查询性能... 目录1.AlloyDBjavascript是什么?AlloyDB 的工作原理2.搭建测试环境3.代码工程1.