////Created by wangtouwang on 15/4/7.//Copyright (c) 2015年 wangtouwang. All rights reserved.//#import<UIKit/UIKit.h>@classYYLockView;@PRotocolYYLockViewDelegate <NSObject>//自定义一个协议//协议方法,把当前视图作为参数-(void)LockViewDidClick:(YYLockView *)lockView andPwd:(NSString *)pwd;@end@interfaceYYLockView : UIView//代理@property(nonatomic,weak) IBOutletid<YYLockViewDelegate>delegate;@end
////YYLockView.m//Created by wangtouwang on 15/4/7.//Copyright (c) 2015年 wangtouwang. All rights reserved.//#import"YYLockView.h"//设置获取屏幕长宽全局变量#defineKSCREEN_WIDTH [UIScreen mainScreen].bounds.size.width#defineKSCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height#defineBUTTON_HEIGHT_MIDDLE_WIDTH_RADIO 1.22@interfaceYYLockView ()
{
UIView*newView;
}
@property(nonatomic,strong)NSMutableArray*buttons;
@property(nonatomic,strong)NSMutableArray*includButtons;//定义一个属性,记录当前点@property(nonatomic,assign)CGPoint currentPoint;@end@implementationYYLockView#pragmamark-懒加载
-(NSMutableArray *)buttons
{if(_buttons==nil) {
_buttons=[NSMutableArray array];
}return_buttons;
}#pragmamark-懒加载
-(NSMutableArray *)includButtons
{if(_includButtons==nil) {
_includButtons=[NSMutableArray array];
}return_includButtons;
}-(id)initWithCoder:(NSCoder *)aDecoder
{
NSLog(@"断点 B 号");if(self=[super initWithCoder:aDecoder]) {
[self setup];
}returnself;
}//界面搭建- (id)initWithFrame:(CGRect)frame
{
NSLog(@"断点 1 号");
self=[super initWithFrame:frame];if(self) {
[self setup];
}returnself;
}//在界面上创建9个按钮-(void)setup
{//NSLog(@"断点 2 号 ");//newView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];//newView.backgroundColor=[UIColor redColor];//[self addSubview:newView];//1.创建9个按钮for(inti=0; i<9; i++) {
UIButton*btn=[UIButton buttonWithType:UIButtonTypeCustom];//2.设置按钮的状态背景[btn setBackgroundImage:[UIImage imageNamed:@"gesture_node_normal"] forState:UIControlStateNormal];
[btn setBackgroundImage:[UIImage imageNamed:@"gesture_node_highlighted"] forState:UIControlStateSelected];//3.把按钮添加到视图中[self addSubview:btn];//将按钮添加到包含按钮数组中[[self includButtons] addObject:btn];//4.禁止按钮的点击事件btn.userInteractionEnabled=NO;//5.设置每个按钮的tagbtn.tag=i+1;
}
}//4.设置按钮的frame-(void)layoutSubviews
{
NSLog(@"断点顺序 3 号");
CGFloat inverst= KSCREEN_HEIGHT/4.5;
inverst=0;//4.1需要先调用父类的方法[super layoutSubviews];for(inti=0; i<[self includButtons].count; i++) {//4.2取出按钮UIButton *btn=[self includButtons][i];//4.3九宫格法计算每个按钮的frameCGFloat row = i/3;
CGFloat loc= i%3;//NSLog(@"ROW=%f LOC=%f",row,loc);CGFloat btnW=74;
CGFloat btnH=74;
CGFloat padding=(self.frame.size.width-3*btnW)/4;
CGFloat btnX=padding+(btnW+padding)*loc;
CGFloat btnY=padding*BUTTON_HEIGHT_MIDDLE_WIDTH_RADIO+(btnW+padding)*row;//NSLog(@"BTNX-%f BTNY=%f ",btnX,btnY);btn.frame=CGRectMake(btnX, btnY+inverst, btnW, btnH);
}
}//重写drawrect:方法-(void)drawRect:(CGRect)rect
{
NSLog(@"断点顺序 4 号");//获取上下文CGContextRef ctx=UIGraphicsGetCurrentContext();//在每次绘制前,清空上下文CGContextClearRect(ctx, rect);//填充画布颜色[[self renderImageWithColor:[UIColor grayColor] inSize:CGSizeMake(KSCREEN_WIDTH, KSCREEN_HEIGHT)] drawInRect:CGRectMake(0,0, KSCREEN_WIDTH, KSCREEN_HEIGHT)];//在坐标中画出图片//绘图(线段)for(inti=0; i<self.buttons.count; i++) {
UIButton*btn=self.buttons[i];if(0==i) {//设置起点(注意连接的是中点)//CGContextMoveToPoint(ctx, btn.frame.origin.x, btn.frame.origin.y);CGContextMoveToPoint(ctx, btn.center.x, btn.center.y);
}else{//CGContextAddLineToPoint(ctx, btn.frame.origin.x, btn.frame.origin.y);CGContextAddLineToPoint(ctx, btn.center.x, btn.center.y);
}
}//当所有按钮的中点都连接好之后,再连接手指当前的位置//判断数组中是否有按钮,只有有按钮的时候才绘制if(self.buttons.count !=0) {
CGContextAddLineToPoint(ctx, self.currentPoint.x, self.currentPoint.y);
}//渲染//设置线条的属性CGContextSetLineWidth(ctx,10);
CGContextSetLineJoin(ctx, kCGLineJoinRound);
CGContextSetLineCap(ctx, kCGLineCapRound);
CGContextSetRGBStrokeColor(ctx,255/255.0,0/255.0,0/255.0,1);
CGContextStrokePath(ctx);
}//填充画布颜色- (UIImage *)renderImageWithColor:(UIColor *)color inSize:(CGSize)size
{
CGRect rect= CGRectMake(0.0f,0.0f, size.width, size.height);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context=UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, rect);
UIImage*image =UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();returnimage;
}//5.监听手指的移动-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
NSLog(@"断点 E 号");
CGPoint starPoint=[self getCurrentPoint:touches];
UIButton*btn=[self getCurrentBtnWithPoint:starPoint];if(btn && btn.selected !=YES) {
btn.selected=YES;
[self.buttons addObject:btn];
}//[self setNeedsDisplay];}-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
NSLog(@"断点 F 号");
CGPoint movePoint=[self getCurrentPoint:touches];
UIButton*btn=[self getCurrentBtnWithPoint:movePoint];//存储按钮//已经连过的按钮,不可再连if(btn && btn.selected !=YES) {//设置按钮的选中状态btn.selected=YES;//把按钮添加到数组中[self.buttons addObject:btn];
}//记录当前点(不在按钮的范围内)self.currentPoint=movePoint;//通知view重新绘制[self setNeedsDisplay];
}//手指离开的时候清除线条-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
NSLog(@"断点 G 号");//取出用户输入的密码//创建一个可变的字符串,用来保存用户密码NSMutableString *result=[NSMutableStringstring];for(UIButton *btninself.buttons) {
[result appendFormat:@"%lu",btn.tag];
}
NSLog(@"用户输入的密码为:%@",result);//通知代理,告知用户输入的密码if([self.delegaterespondsToSelector:@selector(LockViewDidClick:andPwd:)]) {
[self.delegateLockViewDidClick:self andPwd:result];
}//重置按钮的状态//for (UIButton *btn in self.buttons) {//btn.selected=NO;//// [btn setSelected:NO];//}//调用该方法,它就会让数组中的每一个元素都调用setSelected:方法,并给每一个元素传递一个NO参数[self.buttons makeObjectsPerformSelector:@selector(setSelected:) withObject:@(NO)];//清空数组[self.buttons removeAllObjects];
[self setNeedsDisplay];//清空当前点self.currentPoint=CGPointZero;
}//对功能点进行封装-(CGPoint)getCurrentPoint:(NSSet *)touches
{
NSLog(@"断点 H 号");
UITouch*touch=[touches anyObject];
CGPoint point=[touch locationInView:touch.view];returnpoint;
}-(UIButton *)getCurrentBtnWithPoint:(CGPoint)point
{
NSLog(@"断点 J 号");for(inti=0;i<_includButtons.count;i++) {
UIButton*btn =_includButtons[i];if(CGRectContainsPoint(btn.frame, point)) {returnbtn;
}
}returnNil;
}@end