//// Created by wangtouwang on 15/4/7.// Copyright (c) 2015年 wangtouwang. All rights reserved.//#import <UIKit/UIKit.h>@class YYLockView;@PRotocol YYLockViewDelegate <NSObject>//自定義一個(gè)協(xié)議 //協(xié)議方法,把當(dāng)前視圖作為參數(shù) -(void)LockViewDidClick:(YYLockView *)lockView andPwd:(NSString *)pwd;@end@interface YYLockView : UIView//代理@property(nonatomic,weak) IBOutlet id<YYLockViewDelegate>delegate;@end
//// YYLockView.m// Created by wangtouwang on 15/4/7.// Copyright (c) 2015年 wangtouwang. All rights reserved.//#import "YYLockView.h"// 設(shè)置獲取屏幕長(zhǎng)寬全局變量#define KSCREEN_WIDTH [UIScreen mainScreen].bounds.size.width#define KSCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height#define BUTTON_HEIGHT_MIDDLE_WIDTH_RADIO 1.22@interface YYLockView (){ UIView *newView;}@property(nonatomic,strong)NSMutableArray *buttons;@property(nonatomic,strong)NSMutableArray *includButtons;//定義一個(gè)屬性,記錄當(dāng)前點(diǎn)@property(nonatomic,assign)CGPoint currentPoint;@end@implementation YYLockView#pragma mark-懶加載-(NSMutableArray *)buttons{ if (_buttons==nil) { _buttons=[NSMutableArray array]; } return _buttons;}#pragma mark-懶加載-(NSMutableArray *)includButtons{ if (_includButtons==nil) { _includButtons=[NSMutableArray array]; } return _includButtons;}-(id)initWithCoder:(NSCoder *)aDecoder{ NSLog(@"斷點(diǎn) B 號(hào) "); if (self=[super initWithCoder:aDecoder]) { [self setup]; } return self;}//界面搭建- (id)initWithFrame:(CGRect)frame{ NSLog(@"斷點(diǎn) 1 號(hào) "); self = [super initWithFrame:frame]; if (self) { [self setup]; } return self;}//在界面上創(chuàng)建9個(gè)按鈕-(void)setup{// NSLog(@"斷點(diǎn) 2 號(hào) ");// newView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];// newView.backgroundColor=[UIColor redColor];// [self addSubview:newView]; //1.創(chuàng)建9個(gè)按鈕 for (int i=0; i<9; i++) { UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom]; //2.設(shè)置按鈕的狀態(tài)背景 [btn setBackgroundImage:[UIImage imageNamed:@"gesture_node_normal"] forState:UIControlStateNormal]; [btn setBackgroundImage:[UIImage imageNamed:@"gesture_node_highlighted"] forState:UIControlStateSelected]; //3.把按鈕添加到視圖中 [self addSubview:btn]; //將按鈕添加到包含按鈕數(shù)組中 [[self includButtons] addObject:btn]; //4.禁止按鈕的點(diǎn)擊事件 btn.userInteractionEnabled=NO; //5.設(shè)置每個(gè)按鈕的tag btn.tag=i+1; }}//4.設(shè)置按鈕的frame-(void)layoutSubviews{ NSLog(@"斷點(diǎn)順序 3 號(hào) "); CGFloat inverst = KSCREEN_HEIGHT/4.5; inverst=0; //4.1需要先調(diào)用父類的方法 [super layoutSubviews]; for (int i=0; i<[self includButtons].count; i++) { //4.2取出按鈕 UIButton *btn=[self includButtons][i]; //4.3九宮格法計(jì)算每個(gè)按鈕的frame CGFloat 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(@"斷點(diǎn)順序 4 號(hào) "); //獲取上下文 CGContextRef ctx=UIGraphicsGetCurrentContext(); //在每次繪制前,清空上下文 CGContextClearRect(ctx, rect); //填充畫布顏色 [[self renderImageWithColor:[UIColor grayColor] inSize:CGSizeMake(KSCREEN_WIDTH, KSCREEN_HEIGHT)] drawInRect:CGRectMake(0, 0, KSCREEN_WIDTH, KSCREEN_HEIGHT)];//在坐標(biāo)中畫出圖片 //繪圖(線段) for (int i=0; i<self.buttons.count; i++) { UIButton *btn=self.buttons[i]; if (0==i) { //設(shè)置起點(diǎn)(注意連接的是中點(diǎn)) // 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); } } //當(dāng)所有按鈕的中點(diǎn)都連接好之后,再連接手指當(dāng)前的位置 //判斷數(shù)組中是否有按鈕,只有有按鈕的時(shí)候才繪制 if (self.buttons.count !=0) { CGContextAddLineToPoint(ctx, self.currentPoint.x, self.currentPoint.y); } //渲染 //設(shè)置線條的屬性 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(); return image;}//5.監(jiān)聽手指的移動(dòng)-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ NSLog(@"斷點(diǎn) E 號(hào) "); 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(@"斷點(diǎn) F 號(hào) "); CGPoint movePoint=[self getCurrentPoint:touches]; UIButton *btn=[self getCurrentBtnWithPoint:movePoint]; //存儲(chǔ)按鈕 //已經(jīng)連過的按鈕,不可再連 if (btn && btn.selected != YES) { //設(shè)置按鈕的選中狀態(tài) btn.selected=YES; //把按鈕添加到數(shù)組中 [self.buttons addObject:btn]; } //記錄當(dāng)前點(diǎn)(不在按鈕的范圍內(nèi)) self.currentPoint=movePoint; //通知view重新繪制 [self setNeedsDisplay];}//手指離開的時(shí)候清除線條-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{ NSLog(@"斷點(diǎn) G 號(hào) "); //取出用戶輸入的密碼 //創(chuàng)建一個(gè)可變的字符串,用來保存用戶密碼 NSMutableString *result=[NSMutableString string]; for (UIButton *btn in self.buttons) { [result appendFormat:@"%lu",btn.tag]; } NSLog(@"用戶輸入的密碼為:%@",result); //通知代理,告知用戶輸入的密碼 if ([self.delegate respondsToSelector:@selector(LockViewDidClick:andPwd:)]) { [self.delegate LockViewDidClick:self andPwd:result]; } //重置按鈕的狀態(tài) // for (UIButton *btn in self.buttons) { // btn.selected=NO; //// [btn setSelected:NO]; // } //調(diào)用該方法,它就會(huì)讓數(shù)組中的每一個(gè)元素都調(diào)用setSelected:方法,并給每一個(gè)元素傳遞一個(gè)NO參數(shù) [self.buttons makeObjectsPerformSelector:@selector(setSelected:) withObject:@(NO)]; //清空數(shù)組 [self.buttons removeAllObjects]; [self setNeedsDisplay]; //清空當(dāng)前點(diǎn) self.currentPoint=CGPointZero;}//對(duì)功能點(diǎn)進(jìn)行封裝-(CGPoint)getCurrentPoint:(NSSet *)touches{ NSLog(@"斷點(diǎn) H 號(hào) "); UITouch *touch=[touches anyObject]; CGPoint point=[touch locationInView:touch.view]; return point;}-(UIButton *)getCurrentBtnWithPoint:(CGPoint)point{ NSLog(@"斷點(diǎn) J 號(hào) "); for (int i=0;i<_includButtons.count;i++) { UIButton *btn =_includButtons[i]; if (CGRectContainsPoint(btn.frame, point)) { return btn; } } return Nil;}@end
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注