资讯专栏INFORMATION COLUMN

ionic 2+ 手势解锁界面

Hancock_Xu / 3334人阅读

摘要:手势解锁界面一些对安全要求比较高的少不了锁屏页面,而手势解锁对于用户来说使用方便,对于程序员来说小有挑战,怎么有弃之不用的道理。

ionic 2+ 手势解锁界面

</>复制代码

  1. 一些对安全要求比较高的app少不了锁屏页面,而手势解锁对于用户来说使用方便,对于程序员来说小有挑战,怎么有弃之不用的道理。

效果图
效果图处理短,方便大家阅读

下面附上源码,源码的注释加上语义化代码希望帮助小伙伴加速理解。有不足的地方欢迎小伙伴批评指正。

</>复制代码

  1. import {Component, ElementRef, ViewChild, Renderer2} from "@angular/core";
  2. import {IonicPage, NavController, NavParams} from "ionic-angular";
  3. import {Storage} from "@ionic/storage";
  4. //class
  5. export class Point {
  6. x: number;
  7. y: number;
  8. index?: number;
  9. }
  10. //储存到本地数据库的收拾解锁对象
  11. export class GestureLockObj {
  12. password: string;
  13. chooseType: number;
  14. step: number;
  15. constructor() {
  16. this.chooseType = 3;
  17. this.step = 0;
  18. }
  19. }
  20. //储存到本地数据库的收拾错误对象
  21. export class GestureAttemptObj {
  22. lockDate: number;
  23. lastAttemptDate: number;
  24. attemptsNu: number;
  25. constructor() {
  26. this.attemptsNu = 3;
  27. }
  28. }
  29. @IonicPage()
  30. @Component({
  31. selector: "page-gesture-lock",
  32. templateUrl: "gesture-lock.html",
  33. })
  34. export class GestureLockPage {
  35. height = 320;
  36. width = 320;
  37. chooseType = 3;
  38. devicePixelRatio; // 设备密度
  39. titleMes = "手势密码解锁";
  40. unSelectedColor = "#87888a";
  41. selectedColor = "#1783CE";
  42. successColor = "#7bd56c";
  43. errorColor = "#d54e20";
  44. lockTimeUnit = 50; //尝试失败后锁定多少秒
  45. gestureLockObj: GestureLockObj = new GestureLockObj(); //密码本地缓存
  46. gestureAttemptObj: GestureAttemptObj = new GestureAttemptObj(); //尝试日期和次数本地缓存
  47. firstPassword: string;
  48. private canTouch = false;
  49. private radius: number; //小圆点半径
  50. private allPointArray: Point[] = [];
  51. private unSelectedPointArray: Point[] = [];
  52. private selectedPointArray: Point[] = [];
  53. private ctx;
  54. private lockTime = this.lockTimeUnit;
  55. @ViewChild("canvas") canvas: ElementRef;
  56. textColor = this.selectedColor;
  57. constructor(public navCtrl: NavController,
  58. public navParams: NavParams,
  59. private render: Renderer2,
  60. private storage: Storage) {
  61. }
  62. ngOnInit() {
  63. this.devicePixelRatio = window.devicePixelRatio || 1;
  64. this.radius = this.width * this.devicePixelRatio / (1 + 2 * this.chooseType) / 2; // 半径计算
  65. this.canvas.nativeElement.height = this.height * this.devicePixelRatio;
  66. this.canvas.nativeElement.width = this.width * this.devicePixelRatio;
  67. this.ctx = this.canvas.nativeElement.getContext("2d");
  68. this.initPointArray();
  69. this.ctx.clearRect(0, 0, this.ctx.canvas.width, this.ctx.canvas.height);
  70. this.drawCircles(this.allPointArray);
  71. this.bindEvent();
  72. this.storage.get("gestureLockObj").then(data => {
  73. if (data) {
  74. this.gestureLockObj = data;
  75. }
  76. });
  77. this.storage.get("gestureAttemptObj").then(data => {
  78. if (data) {
  79. this.gestureAttemptObj = data;
  80. if (this.gestureAttemptObj.attemptsNu === 0) {
  81. const now =Date.now();
  82. const last =this.gestureAttemptObj.lockDate;
  83. const secend = (now - last) / 1000 - this.lockTimeUnit;
  84. if (secend <= 0) {
  85. this.setInteralFun( 1- secend );
  86. } else {
  87. this.gestureAttemptObj = new GestureAttemptObj();
  88. this.storage.set("gestureAttemptObj", this.gestureAttemptObj);
  89. }
  90. }
  91. }
  92. });
  93. if (this.gestureLockObj.step === 0) {
  94. this.titleMes = "请绘制你的手势密码";
  95. }
  96. }
  97. //滑动结束后处理密码
  98. private dealPassword(selectedArray) {
  99. if (this.gestureLockObj.step === 2) { /** 进入解锁 **/
  100. if (this.checkPassword(selectedArray, this.gestureLockObj.password)) { // 解锁成功
  101. this.textColor = this.successColor;
  102. this.titleMes = "解锁成功";
  103. this.drawAll(this.successColor);
  104. this.storage.remove("gestureAttemptObj")
  105. } else { //解锁失败
  106. this.lockFaile();
  107. }
  108. } else if (this.gestureLockObj.step === 1) { // 设置密码确认密码
  109. if (this.checkPassword(selectedArray, this.firstPassword)) { //设置密码成功
  110. this.gestureLockObj.step = 2;
  111. this.gestureLockObj.password = this.firstPassword;
  112. this.titleMes = "手势密码设置成功,再次绘制登录";
  113. this.storage.set("gestureLockObj", this.gestureLockObj);
  114. this.drawAll(this.successColor);
  115. } else { //设置密码失败
  116. this.textColor = this.errorColor;
  117. this.titleMes = "两次不一致,重新输入";
  118. this.drawAll(this.errorColor);
  119. this.gestureLockObj = new GestureLockObj();
  120. }
  121. } else if (this.gestureLockObj.step === 0) { //设置密码
  122. this.gestureLockObj.step = 1;
  123. this.firstPassword = this.parsePassword(selectedArray);
  124. this.textColor = this.selectedColor;
  125. this.titleMes = "再次输入";
  126. } else if (this.gestureLockObj.step === 3) {//重置密码输入旧秘密
  127. if (this.checkPassword(selectedArray, this.gestureLockObj.password)) { // 旧密码成功
  128. this.gestureLockObj.step = 0;
  129. this.textColor = this.successColor;
  130. this.titleMes = "请输入新手势密码";
  131. this.drawAll(this.successColor);
  132. } else { //旧密码失败
  133. this.lockFaile();
  134. }
  135. }
  136. }
  137. //解锁失败
  138. lockFaile() {
  139. this.drawAll(this.errorColor);
  140. this.textColor = this.errorColor;
  141. this.gestureAttemptObj.attemptsNu = this.gestureAttemptObj.attemptsNu - 1;
  142. if (this.gestureAttemptObj.attemptsNu > 0) {
  143. this.titleMes = `密码错误,您还可以输入${this.gestureAttemptObj.attemptsNu}次`;
  144. } else {
  145. this.gestureAttemptObj.lockDate = Date.now();
  146. this.storage.set("gestureAttemptObj", this.gestureAttemptObj);
  147. this.setInteralFun(this.lockTimeUnit);
  148. }
  149. }
  150. setInteralFun(time) { //检查是否超过设定时间
  151. this.lockTime = time;
  152. const interval = setInterval(() => {
  153. this.lockTime = this.lockTime - 1;
  154. this.titleMes = `请在${this.lockTime}秒后尝试`;
  155. if (this.lockTime === 0) {
  156. this.gestureAttemptObj = new GestureAttemptObj();
  157. this.storage.set("gestureAttemptObj", this.gestureAttemptObj);
  158. this.lockTime = this.lockTimeUnit;
  159. this.titleMes = "手势密码解锁";
  160. clearInterval(interval);
  161. }
  162. }, 1000);
  163. }
  164. //重置手势秘密
  165. resetPasswordFun() {
  166. this.titleMes = "请输入旧手势密码";
  167. this.gestureLockObj.step = 3;
  168. }
  169. deletPasswordFun() {
  170. this.storage.remove("gestureLockObj");
  171. this.gestureLockObj = new GestureLockObj();
  172. this.titleMes = "请绘制你的手势密码";
  173. this.reset();
  174. }
  175. //设置手势密码矩阵
  176. setChooseType(type) {
  177. this.chooseType = type;
  178. }
  179. //初始化手势点的坐标数组
  180. private initPointArray() {
  181. const n = this.chooseType;
  182. const radius = this.radius;
  183. this.selectedPointArray = [];
  184. this.allPointArray = [];
  185. this.unSelectedPointArray = [];
  186. for (let i = 0; i < n; i++) {
  187. for (let j = 0; j < n; j++) {
  188. const obj = {
  189. x: (j * 4 + 3) * radius,
  190. y: (i * 4 + 3) * radius,
  191. index: ((i * n + 1 + j) + 2) * 7 - 1
  192. };
  193. this.allPointArray.push(obj);
  194. this.unSelectedPointArray.push(obj);
  195. }
  196. }
  197. }
  198. //滑动手势的时候更新画布
  199. private update(nowPoint: Point) {
  200. this.drawAll(this.selectedColor, nowPoint);
  201. this.dealPoint(this.unSelectedPointArray, nowPoint);
  202. }
  203. private checkPassword(pointArray, password): boolean {
  204. return password === this.parsePassword(pointArray);
  205. }
  206. private parsePassword(pointArray): string {
  207. return pointArray.map(data => {
  208. return data.index;
  209. }).join("");
  210. }
  211. //获得手指滑动点的位置
  212. private getPosition(e): Point {
  213. const rect = e.currentTarget.getBoundingClientRect();
  214. return {
  215. x: (e.touches[0].clientX - rect.left) * this.devicePixelRatio,
  216. y: (e.touches[0].clientY - rect.top) * this.devicePixelRatio
  217. };
  218. }
  219. //重置
  220. reset() {
  221. this.initPointArray();
  222. this.ctx.clearRect(0, 0, this.ctx.canvas.width, this.ctx.canvas.height);
  223. this.drawCircles(this.allPointArray);
  224. }
  225. //添加滑动监听事件
  226. private bindEvent() {
  227. this.render.listen(this.canvas.nativeElement, "touchstart", (e) => {
  228. e.preventDefault();
  229. if (this.selectedPointArray.length === 0 && this.gestureAttemptObj.attemptsNu !== 0) {
  230. this.dealPoint(this.allPointArray, this.getPosition(e), true);
  231. }
  232. });
  233. this.render.listen(this.canvas.nativeElement, "touchmove", (e) => {
  234. if (this.canTouch) {
  235. this.update(this.getPosition(e));
  236. }
  237. });
  238. const self = this;
  239. this.render.listen(this.canvas.nativeElement, "touchend", () => {
  240. if (this.canTouch) {
  241. this.canTouch = false;
  242. this.dealPassword(this.selectedPointArray);
  243. setTimeout(function () {
  244. self.reset();
  245. }, 1000);
  246. }
  247. });
  248. }
  249. //绘制滑动屏幕后的点
  250. private drawAll(color, nowPoint = null) {
  251. this.ctx.clearRect(0, 0, this.ctx.canvas.width, this.ctx.canvas.height);
  252. this.drawCircles(this.allPointArray);
  253. this.drawCircles(this.selectedPointArray, color);
  254. this.drawPoints(this.selectedPointArray, color);
  255. this.drawLine(this.selectedPointArray, color, nowPoint);
  256. }
  257. //滑动点的时候处理是否划中点
  258. private dealPoint(pointArry: Point[], nowPoint: Point, canTouch = false) {
  259. for (let i = 0; i < pointArry.length; i++) {
  260. if (Math.abs(Number(nowPoint.x) - Number(pointArry[i].x)) < this.radius && Math.abs(Number(nowPoint.y) - Number(pointArry[i].y)) < this.radius) {
  261. if (canTouch) {
  262. this.canTouch = true;
  263. }
  264. this.drawPoint(pointArry[i]);
  265. this.selectedPointArray.push(pointArry[i]);
  266. this.unSelectedPointArray.splice(i, 1);
  267. break;
  268. }
  269. }
  270. }
  271. private drawPoints(pointArray: Point[], style = this.selectedColor) {
  272. for (const value of pointArray) {
  273. this.drawPoint(value, style);
  274. }
  275. }
  276. private drawCircles(pointArray: Point[], style = this.unSelectedColor) {
  277. for (const value of pointArray) {
  278. this.drawCircle(value, style);
  279. }
  280. }
  281. //画圈
  282. private drawCircle(point: Point, style = this.unSelectedColor) {
  283. this.ctx.strokeStyle = style;
  284. this.ctx.lineWidth = 2;
  285. this.ctx.beginPath();
  286. this.ctx.arc(point.x, point.y, this.radius, 0, Math.PI * 2, true);
  287. this.ctx.closePath();
  288. this.ctx.stroke();
  289. }
  290. //画点
  291. private drawPoint(point: Point, style = this.selectedColor) {
  292. this.ctx.fillStyle = style;
  293. this.ctx.beginPath();
  294. this.ctx.arc(point.x, point.y, this.radius / 2.5, 0, Math.PI * 2, true);
  295. this.ctx.closePath();
  296. this.ctx.fill();
  297. }
  298. //划线
  299. private drawLine(pointArray: Point[], style, nowPoint: Point = null) {
  300. this.ctx.beginPath();
  301. this.ctx.strokeStyle = style;
  302. this.ctx.lineWidth = 3;
  303. this.ctx.moveTo(pointArray[0].x, pointArray[0].y);
  304. for (let i = 1; i < pointArray.length; i++) {
  305. this.ctx.lineTo(pointArray[i].x, pointArray[i].y);
  306. }
  307. if (nowPoint) {
  308. this.ctx.lineTo(nowPoint.x, nowPoint.y);
  309. }
  310. this.ctx.stroke();
  311. this.ctx.closePath();
  312. }
  313. }

注意 注意
锁屏界面的实现并不复杂,复杂是的是在程序中怎么正确的调用,欢迎小伙伴交流。
源码地址,html+sass+ts
https://github.com/sure2darli...
咯咯哒
感觉这个插件对你有帮助请点个赞赞赞赞吧!!
咯咯哒

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/95050.html

相关文章

  • 一个基于Angular+Ionic+Phonegap的混合APP实战

    摘要:有二维码扫描功能,还做了类似消息可拖拽效果,上拉下拉刷新,轮播图组件。特别适合用于基于模式的移动应用程序开发。简介是一个用基于,和的,创建移动跨平台移动应用程序的快速开发平台。 这个项目做得比较早,当时是基于ionic1和angular1做的。做了四个tabs的app,首页模仿携程首页,第二页主要是phonegap调用手机核心功能,第三页模仿微信和qq聊天页,第四页模仿一般手机的表单设...

    孙淑建 评论0 收藏0
  • 用 canvas 实现 Web 手势解锁

    摘要:画线画线需要借助来完成,也就是,当我们的时候,传入开始时的相对坐标,作为线的一端,当我们的时候,获得坐标,作为线的另一端,当我们的时候,开始画线。有两个函数,获得当前的相对坐标获得触摸点的相对位置相对坐标画线画线然后就是监听的和事件了。 最近参加 360 暑假的前端星计划,有一个在线作业,截止日期是 3 月 30 号,让手动实现一个 H5 手势解锁,具体的效果就像原生手机的九宫格解锁那...

    MycLambert 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<