[IOS]划屏解锁(重写UIView)
来源:互联网 发布:中国数学水平 知乎 编辑:程序博客网 时间:2024/04/30 06:01
[IOS]划屏解锁(重写UIView)
Demo:http://download.csdn.net/detail/u012881779/8667545
1.创建一个继承于UIView的类:PaddleDraw
2.创建一个继承于UIViewController的类ViewController
再往ViewController的Xib中添加控件UIView,设置UIView继承于这个自定义的PaddleDraw
#import <UIKit/UIKit.h>@interface PaddleDraw : UIView<UIAlertViewDelegate >@property (strong, nonatomic)NSMutableArray *pointAdd; //存储被触摸到的button的中心点@property (strong, nonatomic)NSMutableArray *centerPoint; //存储初始化创建的Button中心点@property (strong, nonatomic)NSMutableArray *buttonArr; //存储所有Button@property (strong, nonatomic)NSMutableString *strScore; //第一次设置密码,按触摸顺序@property (strong, nonatomic)NSMutableString *compareScore; //第二次输入密码@property (strong, nonatomic)UIView *topView; //透明玻璃纸//还原Button状态-(void)cleanButton;@end#import "PaddleDraw.h"@implementation PaddleDraw@synthesize pointAdd = _pointAdd;@synthesize centerPoint = _centerPoint;@synthesize buttonArr = _buttonArr;@synthesize strScore = _strScore;@synthesize compareScore = _compareScore;@synthesize topView = _topView;- (id)initWithFrame:(CGRect)frame{ self = [super initWithFrame:frame]; if (self) { // Initialization code } return self;}//自己写一个构造函数,按照上面函数的格式-(id)initWithCoder:(NSCoder *)aDecoder{ self = [super initWithCoder:aDecoder]; if(self){ //存储Button中心点 _centerPoint = nil; if(!_centerPoint){ _centerPoint = [[NSMutableArray alloc] init]; } [_centerPoint removeAllObjects]; //存储所有Button _buttonArr = nil; if(!_buttonArr){ _buttonArr = [[NSMutableArray alloc ] init]; } //定义Button格数 for(int i = 0;i < 20;i ++){ UIButton *screenButton = [[UIButton alloc] init]; [screenButton setFrame:CGRectMake(5+(i%4)*87.6, 100+(i/4)*87.6, 32, 32)]; [screenButton setBackgroundImage:[UIImage imageNamed:@"gray.png"] forState:UIControlStateNormal]; [screenButton setTag:i+1]; CGPoint pointBut = [screenButton center]; [_centerPoint addObject:[NSValue valueWithCGPoint:pointBut]]; [_buttonArr addObject:screenButton]; [self addSubview:screenButton]; } //放一个透明的View接受响应,避免Button吸收响应 if(!_topView){ _topView = [[UIView alloc] initWithFrame:CGRectMake(0, 20, 320, 568)]; [_topView setBackgroundColor:[UIColor clearColor] ]; [self addSubview:_topView]; } } return self; }// Only override drawRect: if you perform custom drawing.- (void)drawRect:(CGRect)rect { // Drawing code if(_pointAdd){ CGContextRef context = UIGraphicsGetCurrentContext(); CGPoint startPoint = [[_pointAdd firstObject] CGPointValue]; CGContextMoveToPoint(context, startPoint.x, startPoint.y); for(int j = 0;j < [_pointAdd count];j ++){ CGPoint tempArr = [[_pointAdd objectAtIndex:j] CGPointValue]; CGContextAddLineToPoint(context, tempArr.x,tempArr.y); } [[UIColor colorWithRed:250/255.0 green:200/255.0 blue:99/255.0 alpha:1] set]; CGContextSetLineWidth(context, 10); CGContextStrokePath(context); }}//还原Button状态-(void)cleanButton{ for(UIButton *tempBut in _buttonArr){ [tempBut setBackgroundImage:[UIImage imageNamed:@"gray.png"] forState:UIControlStateNormal ]; } //密码清空 _strScore = nil; //刷新,重新显示 [self setNeedsDisplay];}#pragma mark UIResponder//开始触摸- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ //还原Button的状态 [self cleanButton];}//触摸移动,不断监听一次有效的Touch移动- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{ CGPoint point = [[touches anyObject] locationInView:self]; if(!_pointAdd){ //触摸经过的Button中心 _pointAdd = [[NSMutableArray alloc] init]; } if(!_strScore){ //密码 _strScore = [[NSMutableString alloc] init]; } //判断触摸在UI上的点,是否在Button上, for(int i = 0;i < 5;i ++){ if(point.y > 100+(i%5)*87.6 && point.y < 100+(i%5)*87.6+32){ for(int j = 0;j < 4;j ++){ if(point.x > 10+(j%4)*87.6 && point.x < 10+(j%4)*87.6+32){ //取得该Button的中心点坐标,传给PointAdd NSMutableArray *aaa = _centerPoint; NSLog(@"___%@",aaa); [_pointAdd addObject:[_centerPoint objectAtIndex:4*i+j]]; //存储密码,Button连接顺序 NSString *strTemp = [NSString stringWithFormat:@"%d",4*i+(j+1)]; NSRange range = [_strScore rangeOfString:strTemp]; //查看当前Button是否已经经过一次, if(range.location == NSNotFound){ [_strScore appendString:strTemp]; } //取得Button,改变其状态 UIButton *theButton = (UIButton *)[self viewWithTag:4*i+j+1]; [theButton setBackgroundImage:[UIImage imageNamed:@"yellow.png"] forState:UIControlStateNormal]; //改变状态后,跳出for循环 break; } } break; } } [self setNeedsDisplay];}//触摸事件完成- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{ //触摸结束后,清空上次触摸存储到pointAdd中的Button中心坐标数据 [_pointAdd removeAllObjects]; //查看密码,比较第二次输入密码是否相同 if(!_compareScore){ _compareScore = [[NSMutableString alloc] init]; } if(![_strScore isEqualToString:_compareScore]){ UIAlertView *theAlert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"验证密码" delegate:self cancelButtonTitle:@"再次输入" otherButtonTitles:@"取消", nil, nil]; [theAlert show]; }else{ //成功设置,页面跳转 UIAlertView *theAlert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"验证成功" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil, nil]; [theAlert show]; }}//触摸事件取消,比如来电话打断- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{ NSLog(@"touchesCancelled");}#pragma mark UIAlertViewDelegate- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex{ //再次输入 if(buttonIndex == 0){ //查看密码,比较第二次输入密码是否相同 _compareScore = _strScore; [self cleanButton]; } //重新输入 if(buttonIndex == 1){ [self cleanButton]; } }@end
示图:
0 0
- [IOS]划屏解锁(重写UIView)
- [IOS]绘图、写字(重写UIView)
- IOS开发(68)之捕获点击划屏手势
- 重写UIView
- iOS截屏分享二维码(UIView)
- iOS学习小计划
- iOS 开发实战-锁屏界面(密码解锁)
- iOS 开发实战-锁屏界面(手势解锁)
- IOS-UIView
- IOS UIView
- IOS UIView
- iOS UIView
- IOS UIView
- iOS UIView
- iOS UIView
- iOS UIView
- ios手势锁屏、解锁功能开发
- iOS 监听锁屏/解锁事件
- 单例设计模式
- String类
- 搜索引擎的工作原理
- 到一个变量
- java.util.concurrent.RunnableFuture
- [IOS]划屏解锁(重写UIView)
- Android开发:新建Android Application,src和layout文件夹为空(含新建模拟器及模拟器异常问题解决)
- android中的launchMode和flag
- 基于强类型DataSet的批量数据导入的优化算法
- android中的intent
- 黑马程序员-java高新技术前奏-enhance1
- 继续随机数:接受/拒绝方法(标准正态分布)
- 注释的代码
- 叶正盛:再次写给我们这些浮躁的程序员