简单九宫格锁屏功能的实现与封装
首先来看看最后的实现界面。
在这开始看下面的内容之前希望你能先大概思考活着回顾一下如果
你会怎么做,只要知道大概的思路就可以。
由于iphone5指纹解锁的实现是的这个功能呗淘汰,但是你可能会在想,都淘汰了你还在这里说个毛线啊,其实大家都知道,编程注重的思想,当然会了这个你不可能就会了指纹技术,哪还得等笔者在后面的学习中给大家分享,只是或许有一天这种功能或者思路在哪里要用到你不觉得是一件很开心的事情吗,而且如果你是不想自己敲的话直接可以拿来用。
好了不多废话直接上。。。
关于节目的搭建这里就不多说了,那都是简单的不能再简单的东西,这里只是介绍怎么一步步去实现这个功能。
这里我使用的是触摸事件,当然你也可是使用手势识别来实现,思路基本一致:
一:头文件的处理
首先我们在头文件中使用代理的当时传值,只要遵守这个代理,并且实现了这个代理方法,再将对应的View设置为我们自定义的View就可以了。
#import <UIKit/UIKit.h> @class iCocosLockView; @protocol iCocosLockView Delegate <NSObject> @optional - (void)lockView:(iCocosLockView *)lockView didFinishPath:(NSString *)path; @end @interface iCocosLockView : UIView @property (nonatomic, weak) IBOutlet id<YZLockViewDelegate> delegate; @end
二:下面就是实现文件中来一步步的实现这个功能
在私有拓展中定义一下相关属性
#import "iCocosLockView.h" #define kCount 9 @interface iCocosLockView () @property (nonatomic, strong) NSMutableArray *selectedButtons; @property (nonatomic, assign) CGPoint currentMovePoint; @end
第一步:根据定义的那个选中按钮属性实现懒加载
- (NSMutableArray *)selectedButtons { if (_selectedButtons == nil) { _selectedButtons = [NSMutableArray array]; } return _selectedButtons; }
第二部:为了方便拓展,使用代码的方式创建活着使用IB方式创建我们需要事项两个方法,并且在里面实现按钮的初始化
// 通过代码
- (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { // Initialization code [self setUp]; } return self; }
// 通过xib加载
- (id)initWithCoder:(NSCoder *)aDecoder { if (self = [super initWithCoder:aDecoder]) { [self setUp]; } return self; }
// 初始化实现
- (void)setUp { // 创建9个按钮 ;i < kCount; i++) { // 创建按钮 UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom]; btn.userInteractionEnabled = NO; // 设置按钮默认的图片 [btn setImage:[UIImage imageNamed:@"gesture_node_normal"] forState:UIControlStateNormal]; // 设置按钮选中的图片 [btn setImage:[UIImage imageNamed:@"gesture_node_highlighted"] forState:UIControlStateSelected]; btn.tag= i; // 将btn添加到视图 [self addSubview:btn]; } }
第三步:设置按钮的位置,因为在init方法中可能没有frame,在这里设置最准确
- (void)layoutSubviews { [super layoutSubviews]; // 设置按钮的位置 ; i < self.subviews.count; i++) { // 取出按钮 UIButton *btn = self.subviews[i]; // 设置位置 CGFloat w = ; CGFloat h = ; ; CGFloat col = i % totalCol; CGFloat row = i / totalCol; CGFloat margin = (self.bounds.size.width - totalCol * w) / (totalCol + ); CGFloat x = col * (margin + w) + margin; CGFloat y = row * (margin + h); btn.frame = CGRectMake(x, y, w, h); } }
第四步: 根据touches集合获取对应的触摸点位置
- (CGPoint)pointWithTouches:(NSSet *)touches { UITouch *touch = [touches anyObject]; CGPoint pos = [touch locationInView:self]; return pos; }
第五步:根据不同的业务逻辑,划分功能,一个功能里面不要处理太多业务逻辑,以后开发就不好找了。
// 根据触摸点获得对应的按钮
- (UIButton *)buttonWithPoint:(CGPoint)point { ; i < kCount; i++) { UIButton *btn = self.subviews[i]; CGFloat wh = ; CGPoint center = btn.center; CGFloat x = center.x - wh * 0.5; CGFloat y = center.y - wh * 0.5; CGRect r = CGRectMake(x, y, wh, wh); if (CGRectContainsPoint(btn.frame, point)) { return btn; } } // 都没有找到就返回Nil return nil; }
第六步:触摸开始
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { _currentMovePoint = CGPointMake(-, -); // 1.获取触摸点 CGPoint pos =[self pointWithTouches:touches]; // 2.获取触摸的按钮 UIButton *btn = [self buttonWithPoint:pos]; // 3.设置状态 if (btn && btn.selected == NO) { // 摸到按钮 btn.selected = YES; [self.selectedButtons addObject:btn]; } [self setNeedsDisplay]; }
第七步:开始移动
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { // 1.获取触摸点 CGPoint pos =[self pointWithTouches:touches]; // 2.获取触摸的按钮 UIButton *btn = [self buttonWithPoint:pos]; // 3.设置状态 if (btn && btn.selected == NO) { // 摸到按钮 btn.selected = YES; [self.selectedButtons addObject:btn]; }else{ _currentMovePoint = pos; } [self setNeedsDisplay]; }
第八步:开始触摸的时候获取位置
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { // 获取路径 NSMutableString *str = [NSMutableString string]; for (UIButton *b in _selectedButtons) { [str appendFormat:@"%d",b.tag]; } if ([_delegate respondsToSelector:@selector(lockView:didFinishPath:)]) { [_delegate lockView:self didFinishPath:str]; } // 全部取消选中 [self.selectedButtons makeObjectsPerformSelector:@selector(setSelected:) withObject:@(NO)]; // 清空数组 [self.selectedButtons removeAllObjects]; // 重绘 [self setNeedsDisplay]; }
第九步:绘制
- (void)drawRect:(CGRect)rect { // Drawing code ) return; UIBezierPath *path = [UIBezierPath bezierPath]; ; i < self.selectedButtons.count; i++) { UIButton *b = _selectedButtons[i]; ) { [path moveToPoint:b.center]; }else{ [path addLineToPoint:b.center]; } } // 如果不是原点才需要画,但是有时候我们也需要画原点,怎么做,是不是可以搞个默认的初始值啊 // 注意啊,这个初始值不能在初始化里面搞,应该在触摸开始的时候设置 , -))) { [path addLineToPoint:_currentMovePoint]; } path.lineWidth = ; // 设置线段样式 path.lineJoinStyle = kCGLineJoinBevel; [[UIColor greenColor] set]; [path stroke]; }
@end
上面我们就实现了关于九宫格锁屏的疯转,下载使用起来就非常简单了,几行代码搞定:
#import "iCocosLockView.h" @interface iCocosViewController ()<iCocosLockViewDelegate> - (void)lockView:(iCocosLockView *)lockView didFinishPath:(NSString *)path { NSLog(@"拿到解锁路径---%@",path); }
好了,一个很炫的九宫格解锁酒实现了,是不是很酷,,,,