手势解锁
一、代码
1.WMLockerView.m
#import "ViewController.h"
#import "WMLockerView.h"
@interface ViewController ()<WMLockerViewDelegate>
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)lockViewDidClick:(WMLockerView *)lockView andPwd:(NSString *)pwd{
NSLog(@"结果是:%@",pwd);
}
@end
2.WMLockerView.m
#import "WMLockerView.h"
@interface WMLockerView()
@property (nonatomic, strong) NSMutableArray *buttons;
@property (nonatomic, assign) CGPoint currentPoint;
@end
@implementation WMLockerView
- (NSMutableArray *)buttons
{
if (_buttons == nil) {
_buttons = [NSMutableArray array];
}
return _buttons;
}
/**
* 使用代码创建的时候都会调用这个方法
*/
- (instancetype)initWithFrame:(CGRect)frame{
if(self = [super initWithFrame:frame]){
[self setup];
}
return self;
}
/**
* 使用xib或storyboard创建的时候都会调用这个方法
*/
- (instancetype)initWithCoder:(NSCoder *)aDecoder{
if(self = [super initWithCoder:aDecoder]){
[self setup];
}
return self;
}
/**
* 创建9宫格
*/
-(void)setup{
int oneRowCount = 3;
int btnW = 74;
int btnH = 74;
int btnX = 0 ;
int btnY = 0 ;
int margin = ( self.bounds.size.width-(btnH*oneRowCount) )/(oneRowCount+1);
for (int i=0; i<oneRowCount; i++) {
for (int j=0; j<oneRowCount; j++) {
btnX = margin + i*(margin+btnW);
btnY = margin + j*(margin+btnH);
UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setBackgroundImage:[UIImage imageNamed:@"gesture_node_normal"] forState:UIControlStateNormal];
[btn setBackgroundImage:[UIImage imageNamed:@"gesture_node_highlighted"] forState:UIControlStateSelected];
btn.userInteractionEnabled = NO;
[btn setFrame:CGRectMake(btnX, btnY, btnW, btnH)];
btn.tag = (i+1)*10 + (j+1);
[self addSubview:btn];
}
}
}
/**
* 开始触摸
*/
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
CGPoint point = [self getCurrentTouchPoint:touches];
UIButton * btn = [self getCurrentBtnWithPoint:point];
if(btn){
btn.selected = YES;
[self.buttons addObject:btn];
}
}
/**
*移动
*/
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
CGPoint point = [self getCurrentTouchPoint:touches];
UIButton * btn = [self getCurrentBtnWithPoint:point];
if(btn && btn.selected != YES){
btn.selected = YES;
[self.buttons addObject:btn];
}
self.currentPoint =point;
[self setNeedsDisplay];
}
/**
*抬起
*/
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
NSMutableString * result = [NSMutableString string];
for (UIButton *btn in self.buttons) {
[result appendFormat:@"%ld",btn.tag];
}
if ([self.delegate respondsToSelector:@selector(lockViewDidClick:andPwd:)]) {
[self.delegate lockViewDidClick:self andPwd:result];
}
for (UIButton *btn in self.buttons) {
btn.selected = NO;
}
[self.buttons removeAllObjects];
self.currentPoint = CGPointZero;
[self setNeedsDisplay];
}
/**
*绘画
*/
- (void)drawRect:(CGRect)rect {
CGContextRef ctx = UIGraphicsGetCurrentContext();
for (int i=0; i<self.buttons.count; i++) {
UIButton * btn = self.buttons[i];
if (i==0) {
CGContextMoveToPoint(ctx, btn.center.x, btn.center.y);
} else {
CGContextAddLineToPoint(ctx, btn.center.x, btn.center.y);
}
}
if (self.buttons.count>0) {
CGContextAddLineToPoint(ctx, self.currentPoint.x,self.currentPoint.y);
}
[[UIColor greenColor]set];
CGContextSetLineWidth(ctx, 10);
CGContextSetLineJoin(ctx, kCGLineJoinRound);
CGContextStrokePath(ctx);
}
/**
* 根据系统传入的UITouch集合获取当前触摸的点
* @return 当初触摸的点
*/
- (CGPoint)getCurrentTouchPoint:(NSSet *)touches
{
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView:touch.view];
return point;
}
/**
* 根据触摸点获取触摸到的按钮
* @return 触摸的按钮
*/
- (UIButton *)getCurrentBtnWithPoint:(CGPoint)point
{
for (UIButton *btn in self.subviews) {
if (CGRectContainsPoint(btn.frame, point)) {
return btn;
}
}
return nil;
}
@end
二、图示
![这里写图片描述](https://img-blog.csdn.net/20170320173036252?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcWl3ZW5taW5nc2hpd28=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)