手势滑动解锁

2017-03-14  本文已影响19人  CodeFXQ

#import<UIKit/UIKit.h>

@protocol FXQClockViewDelegate<NSObject>

- (void)selectorBtnStr:(NSString *)str;

@end

@interface FXQClockView : UIView

@property (assign, nonatomic) id<FXQClockViewDelegate> delegate;

@end

#import "FXQClockView.h"

@interface FXQClockView()

/** 存放的都是当前选中的按钮 */

@property (nonatomic, strong) NSMutableArray *selectBtnArray;

//当前手指所在的点

@property (nonatomic, assign) CGPoint curP;

@end

@implementation FXQClockView

-(NSMutableArray *)selectBtnArray {    

    if (_selectBtnArray == nil) { 

       _selectBtnArray = [NSMutableArray array];   

 }   

 return _selectBtnArray;

}

-(void)awakeFromNib {  

  [super awakeFromNib]; 

   //搭建界面添加按钮 

   [self setUp];

}

- (void)setUp {     

   for (int i = 0; i < 9; i++) {              

  //创建按钮      

  UIButton *btn= [UIButton buttonWithType:UIButtonTypeCustom];                btn.userInteractionEnabled = NO;        

 btn.tag = i;              

  //设置按钮图片      

  [btn setImage:[UIImage imageNamed:@"gesture_node_normal"] forState:UIControlStateNormal];       

 //设置选中状态下的图片       

 [btn setImage:[UIImage imageNamed:@"gesture_node_selected"] forState:UIControlStateSelected];              

  [self addSubview:btn];            

}       

 }

//获取当前手指所在的点

- (CGPoint)getCurrentPoint:(NSSet *)touches {    

    //1.获取当前手指所在的点   

 UITouch *touch = [touches anyObject];   

 return [touch locationInView:self];  

  }

//给定一个点,判断给定的点在不在按钮身上//如果在按钮身,返回当前所在的按钮,如果不在,返回nil;

- (UIButton *)btnRectContainsPoint:(CGPoint)point {      

  for (UIButton *btn in self.subviews) {         

       if (CGRectContainsPoint(btn.frame, point)) {     

       //让当前按钮成为选中状态        

           return ban;       

 }            

}   

 return nil;

}

-(void)touchesBegan:(NSSet*)touches withEvent:(UIEvent *)event {

//手指离开时调用-(void)touchesEnded:(NSSet*)touches withEvent:(UIEvent *)event {

NSMutableString *str = [NSMutableString string];

//1.取消所有选中的按钮

for (UIButton *btn in self.selectBtnArray) {

btn.selected = NO;

[str appendFormat:@"%ld",btn.tag];

}

//手指移动时调用-(void)touchesMoved:(NSSet*)touches withEvent:(UIEvent *)event {

//当前的手指所在的点在不在按钮上, 如果在,让按钮成为选中状态

//1.获取当前手指所在的点

//UITouch *touch = [touches anyObject];

//CGPoint curP = [touch locationInView:self];

CGPoint curP = [self getCurrentPoint:touches];

//记录当前手指所在的点

self.curP = curP;

//2.判断curP在不在按钮身上

UIButton *btn = [self btnRectContainsPoint:curP];

if (btn && btn.selected == NO) {

btn.selected = YES;

[self.selectBtnArray addObject:btn];

}

//重绘

[self setNeedsDisplay];

}

//2.清空路径

[self.selectBtnArray removeAllObjects];

[self setNeedsDisplay];

//3.查看当前选中按钮的顺序

[self.delegate selectorBtnStr:str];

}

-(void)drawRect:(CGRect)rect {

if (self.selectBtnArray.count) {

//1.创建路径

UIBezierPath *path = [UIBezierPath bezierPath];

//2.取出所有保存的选中的按钮

for(int i = 0; i < self.selectBtnArray.count; i++) {

//取出每一个按钮

UIButton *btn =  self.selectBtnArray[i];

//判断当前按钮是不是第一个按钮

if(i == 0) {

//如果是,设置成路径的起点

[path moveToPoint:btn.center];

}else {

//添加一根线到按钮的中心

[path addLineToPoint:btn.center];

}

}

//添加一根线到当前手指所在的点

[path  addLineToPoint:self.curP];

//设置路径的状态

[path setLineWidth:5];

[[UIColor blueColor] set];

[path setLineJoinStyle:kCGLineJoinRound];

//3.绘制路径

[path stroke];

}

}

-(void)layoutSubviews {

[super layoutSubviews];

//取出每一个按钮,设置按钮的frame.

CGFloat x = 0;

CGFloat y = 0;

CGFloat btnWH = 74;

//总共有多少列

int column = 3;

CGFloat margin = (self.bounds.size.width - (btnWH * column)) / (column + 1);

int curC = 0;

int curR = 0;

for (int i =0; i < self.subviews.count; i++) {

//求当前所在的列

curC = i % column;

//当前所在的行

curR = i / column;

x = margin + (btnWH + margin) * curC;

y = margin + (btnWH + margin) * curR;

//取出每一个按钮

UIButton *btn = self.subviews[i];

//设置按钮的frame.

btn.frame = CGRectMake(x, y, btnWH, btnWH);

}

}

@end

上一篇 下一篇

猜你喜欢

热点阅读