(轉 部分修改) IOS 手勢密碼(簡單版),ios手勢
//// Created by wangtouwang on 15/4/7.// Copyright (c) 2015年 wangtouwang. All rights reserved.//#import <UIKit/UIKit.h>@class YYLockView;@protocol YYLockViewDelegate <NSObject>//自訂一個協議 //協議方法,把當前視圖作為參數 -(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"// 設定擷取螢幕長寬全域變數#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;//定義一個屬性,記錄當前點@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(@"斷點 B 號 "); if (self=[super initWithCoder:aDecoder]) { [self setup]; } return self;}//介面搭建- (id)initWithFrame:(CGRect)frame{ NSLog(@"斷點 1 號 "); self = [super initWithFrame:frame]; if (self) { [self setup]; } return self;}//在介面上建立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 (int i=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.設定每個按鈕的tag btn.tag=i+1; }}//4.設定按鈕的frame-(void)layoutSubviews{ NSLog(@"斷點順序 3 號 "); CGFloat inverst = KSCREEN_HEIGHT/4.5; inverst=0; //4.1需要先調用父類的方法 [super layoutSubviews]; for (int i=0; i<[self includButtons].count; i++) { //4.2取出按鈕 UIButton *btn=[self includButtons][i]; //4.3九宮格法計算每個按鈕的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(@"斷點順序 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 (int i=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(); return image;}//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=[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]; } //重設按鈕的狀態 // 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]; return point;}-(UIButton *)getCurrentBtnWithPoint:(CGPoint)point{ NSLog(@"斷點 J 號 "); for (int i=0;i<_includButtons.count;i++) { UIButton *btn =_includButtons[i]; if (CGRectContainsPoint(btn.frame, point)) { return btn; } } return Nil;}@end