码迷,mamicode.com
首页 > 移动开发 > 详细

iOS开发-高级UI-手势解锁

时间:2015-08-08 14:52:10      阅读:146      评论:0      收藏:0      [点我收藏+]

标签:

一、手势解锁分析
1.搭建界面(一个整屏UIView和一个较小的View上)
2.新建一个类NJLockView,将较小View的Class设置为NJLockView
在NJLockView.m中

//当视图是通过代码创建出来的就会调用initWithFrame:方法
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if(self){
// Initialization cade
[self setup];

}
return self;
}

//当视图从xib或故事板中创建出来就会调用
- (id)initWithCoder:(NSCoder *)aDecoder
{
if(self = [super initWithCoder:aDecoder]){
//创建9个按钮
[self setup];
}
return self;
}

//创建9个按钮添加到自定义view中
-(void)setup
{
for (int i = 0;i<9;i++){
//1.创建按钮
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom] ;
}
//2.设置按钮的背景图片
[btn setBackgroundImage:[UIImage imageNamed:@"gesture_node_normal"] forState:UIControolStateNormal];

[btn setBackgroundImage:[UIImage imageNamed: @"gesture_node_highlighted"] forState:UIControolStateSeclected];

//3.添加按钮到View
[self addObejct:btn];


//4.禁止按钮的点击事件(因为我们需要监听触摸事件)
btn.userInteractionEnabled = NO;

//5.设置按钮的tag作为唯一的标识
btn.tag = i;

}

- (void)layoutSubviews
{
[super layoutSubviews]; //注意!!!一定要设置,否则后果自负
// 设置按钮的frame
for (int i = 0; i<self.subviews.count;i++){
//1.取出对应位置的按钮
UIButton *btn = self.subviews[1];

//2.设置frame
CGFloat btnW = 74;
CGFloat btnH = 74;
//2.1.计算间距
CGFloat margin = (self.frame.size.width - (3 * btnW))/ 4
int col = i % 3;//列号
int row = i / 3;//行号
//间距+列号(或行号)*(按钮的宽度+间距)
CGFloat btnX = margin + col*(btnW+margin);
CGFloat btnY = margin + col*(btnW+margin);
btn frame = CGRectMake(btnX,btnY,btnW,btnH)
   }

}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
//1.获取按下的点
CGPoint startPoint = [self getCurrentTouchPoint:touches];
//2.获取触摸的按钮
UIButton *btn = [self getCurrentBtnWithPoint:startPoint];
//存储按钮
if(btn)
{
//设置选中状态
btn.selected = YES;
//将按钮保存到数组中
[self.buttons addObject:btn];
  }
}

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

//1.获取按下的点
CGPoint movepoint = [self getCurrentTouchPoint:touches];
//2.获取触摸的按钮
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
{
//取出用户输入的密码
NSMutableString *result = [NSMutableString string];
for (UIButton *btn in self.buttons){
result appendFormat:@“%d”,btn.tag];
}
//通知代理,告诉代理用户输入的密码
if(self.delegate respondsToSelector:@selector
(lockViewDidClick:andPwd:)]){
[self.delegate lockViewDidClick:self andPwd:result];
}
//恢复按钮的高亮状态
[self.buttons makeObjectsPerformSelector:@selector(setSelected:)
withObject:@(NO)];
//清空数组
[self.buttons removeAllObjects];
[self setNeedsDisplay];
//清空currentPoint
self.currentPoint = CGPointZero;

}

 

//将"获取按下的点"这个功能封装起来
//根据系统传入的UITouch集合获取当前触摸的点
-(CGPoint)getCurrentTouchPoint:(NSSet *)touches
{
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView:touch.view];
return point;
}

//将"判断触摸的位置是否在按钮的范围内"这个功能封装起来
//根据触摸点获取触摸到的按钮
-(UIButton *)getCurrentBtnWithPoint:(CGPoint)point
{
for (UIButton *btn in self.subviews){
if CGRectContainsPoint(btn.frame,point){
return btn;
}
}
return nil;
}

//在类扩展中声明一个数组用来装用户按到的按钮
@property (nonatomic,strong)NSMutableArray *buttnons;
//懒加载
-(NSMutableArray *)buttons
{
if(_buttnons == nil ){
_buttons = [NSMutableArray array];
}
return _buttons;
}

//只要重写了drawRect:方法,那么View的默认背景颜色就是黑色的。
-(void)drawRect:(CGRect)rect
{
//因为默认背景颜色时会有缓存,所以先清空上下文
CGContextClearRect(ctx,rect);
//从数组中取出所有的按钮,连接所有按钮的中点
注意:这里不能用for - in,因为要设置起点和终点。需要判断i,直接for - in就不能
判断i了
CGContextRef ctx = UIGraphicsGetCurrentContext();
for(int i = 0;i<self.buttons.count;i++ ){
//取出按钮
UIButton *btn = self.buttons[i];
if(0 == i){
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 colorWithRed:18/255.0 green:102/255.0 blue:72/255.0 alpha :0.5] set];
CGContextSetLineWidth(ctx,10);
CGContextSetLineJoin(ctx,kCGLineJoinRound);
CGContextSetLineCap(ctx,kCGLineCapRound);
CGContextStrokePath(ctx);
}

在类扩展中新增一个CGPoint属性(记录用户当前手指的位置[非按钮范围内])
@property(nonatomic,assign)CGPoint currentPoint;

3.在NJLockView.h中创建一个协议和一个属性
@class NJLockView
@protocol NJLockViewDelegata<NSObject>
-(void)lockViewDidClick:(NJLockView *)lockView andPwd:(NSStirng *)pwd;
@end

 

@interfaceNJLockView:UIView

@property(nonatomic,weak)IBOutlet id<NJLockViewDelegate> delegate ;

4.在控制器实现文件中遵守协议并实现方法
@interface NJViewController ()<NJLockViewDelegate>

-(void)lockViewDidClick:(NJLockView *)lockView andPwd:(NSStirng *)pwd
{
      NSLog(@"MJViewController %@",pwd);
}

iOS开发-高级UI-手势解锁

标签:

原文地址:http://www.cnblogs.com/marshall-yin/p/4713067.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!