iOS仿邮箱大师的九宫格手势密码解锁
程序员文章站
2023-12-09 22:28:40
本文实例为大家分享了ios手势密码解锁的相关代码,供大家参考,具体内容如下
//
// lockview.m
// 手势解锁
//
// created...
本文实例为大家分享了ios手势密码解锁的相关代码,供大家参考,具体内容如下
// // lockview.m // 手势解锁 // // created by daniel on 16/4/4. // copyright © 2016年 daniel. all rights reserved. // #import "lockview.h" @interface lockview () /** 保存已选中的按钮 */ @property(nonatomic, strong) nsmutablearray *selectedbtn; /** 当前触摸点 */ @property(nonatomic, assign) cgpoint curp; @end @implementation lockview - (nsmutablearray *)selectedbtn { if (_selectedbtn == nil) { _selectedbtn = [nsmutablearray array]; } return _selectedbtn; } - (ibaction)pan:(uipangesturerecognizer *)pan { //获取当前触摸点 _curp = [pan locationinview:self]; //判断触摸点在不在按钮上 for (uibutton *btn in self.subviews) { //如果在按钮上就设置选中状态 //触摸点必须在button中心点30范围内才选中,更精确,体验更好 cgrect rect = cgrectmake(btn.center.x, btn.center.y, 30, 30); if (cgrectcontainspoint(rect, _curp) && btn.selected == no) { btn.selected = yes; //将这个选中的按钮保存起来 [self.selectedbtn addobject:btn]; } } //重绘,调用drawrect方法 [self setneedsdisplay]; //手指抬起时 if(pan.state == uigesturerecognizerstateended) { nsmutablestring *strm = [nsmutablestring string]; //如果当前触摸点不在button上,则手指抬起时,就显示选中的button以及连线 uibutton *lastbtn = [self.selectedbtn lastobject]; //把最后一个选中按钮的中心点设置为当前触摸点,清除最后多出来的一截连线 _curp = lastbtn.center; for (uibutton *btn in self.selectedbtn) { //保存手势密码 [strm appendformat:@"%ld", btn.tag]; } //strm就是密码了 nslog(@"%@",strm); //todo:对比之前保存的密码,如果对了就直接跳转界面了 //nslog(@"手指抬起"); //这里了执行完之后系统自动调用了重绘方法 //等待2s后清除所有连线,清除button的选中状态 dispatch_after(dispatch_time(dispatch_time_now, (int64_t)(1.2 * nsec_per_sec)), dispatch_get_main_queue(), ^{ //取消按钮选中 //[self.selectedbtn makeobjectsperformselector:@selector(setselected:) withobject:no];这句不知道为啥没有效果,只好用循环了 for (uibutton *btn in self.selectedbtn) { [btn setselected:no]; } //清除连线,清空选中按钮 [self.selectedbtn removeallobjects]; [self setneedsdisplay]; }); } } - (void)drawrect:(cgrect)rect { nsinteger count = self.selectedbtn.count; //如果没有按钮被选中,就不画线 if (count == 0) { return; } uibezierpath *path = [uibezierpath bezierpath]; //把所有点都连完线 for (int i = 0; i < count; i++) { uibutton * btn = self.selectedbtn[i]; if (i == 0) { //如果是第一个点,就设置为起点 [path movetopoint:btn.center]; }else { //添加连线 [path addlinetopoint: btn.center]; } } //画最后一个点到手指触摸点之间的线 [path addlinetopoint:_curp]; [[uicolor greencolor]set]; path.linejoinstyle = kcglinejoinround; path.linewidth = 8; [path stroke]; } - (void)awakefromnib { //创建9个按钮 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_highlighted"] forstate:uicontrolstateselected]; [self addsubview:btn]; } } - (void)layoutsubviews { [super layoutsubviews]; //布局子控件 nsinteger count = self.subviews.count; //列数 int cols = 3; cgfloat x = 0; cgfloat y = 0; cgfloat w = 74; cgfloat h = 74; //间距 cgfloat margin = (self.bounds.size.width - w * cols) / (cols + 1); cgfloat col = 0; cgfloat row = 0; for (nsinteger i = 0; i < count; i++) { uibutton *btn = self.subviews[i]; //计算当前button的列行以及xy值 col = i % cols; row = i / cols; x = margin + col * (margin + w); y = row * (margin + w); btn.frame = cgrectmake(x, y, w, h); } } @end
效果图:
以上就是本文的全部内容,希望对大家的学习有所帮助。
推荐阅读