微信小程序开发之手势解锁的介绍
手势解锁是app上常见的解锁方式,相比输入密码方式操作起来要方便许多。这篇文章主要介绍了微信小程序开发教程-手势解锁实例,有兴趣的可以了解一下。
手势解锁是app上常见的解锁方式,相比输入密码方式操作起来要方便许多。下面展示如何基于微信小程序实现手机解锁。最终实现效果如下图:
整个功能基于canvas实现,首先添加画布组件,并设定样式
<!--index.wxml--> <view class="container"> <canvas canvas-id="id-gesture-lock" class="gesture-lock" bindtouchstart="onTouchStart" bindtouchmove="onTouchMove" bindtouchend="onTouchEnd"></canvas> </view>
.gesture-lock { margin: 100rpx auto; width: 300px; height: 300px; background-color: #ffffff; }
手势解锁实现代码在gesture_lock.js中(完整源码地址见末尾)。
初始化
constructor(canvasid, context, cb, opt){ this.touchPoints = []; this.checkPoints = []; this.canvasid = canvasid; this.ctx = context; this.width = opt && opt.width || 300; //画布长度 this.height = opt && opt.height || 300; //画布宽度 this.cycleNum = opt && opt.cycleNum || 3; this.radius = 0; //触摸点半径 this.isParamOk = false; this.marge = this.margeCircle = 25; //触摸点及触摸点和画布边界间隔 this.initColor = opt && opt.initColor || '#C5C5C3'; this.checkColor = opt && opt.checkColor || '#5AA9EC'; this.errorColor = opt && opt.errorColor || '#e19984'; this.touchState = "unTouch"; this.checkParam(); this.lastCheckPoint = null; if (this.isParamOk) { // 计算触摸点的半径长度 this.radius = (this.width - this.marge * 2 - (this.margeCircle * (this.cycleNum - 1))) / (this.cycleNum * 2) this.radius = Math.floor(this.radius); // 计算每个触摸点的圆心位置 this.calCircleParams(); } this.onEnd = cb; //滑动手势结束时的回调函数 }
主要设置一些参数,如canvas的长宽,canvas的context,手势锁的个数(3乘3, 4乘4),手势锁的颜色,手势滑动结束时的回调函数等。并计算出手势锁的半径。
计算每个手势锁的圆心位置
calCircleParams() { let n = this.cycleNum; let count = 0; for (let i = 0; i < n; i++) { for (let j = 0; j < n; j++){ count++; let touchPoint = { x: this.marge + i * (this.radius * 2 + this.margeCircle) + this.radius, y: this.marge + j * (this.radius * 2 + this.margeCircle) + this.radius, index: count, check: "uncheck", } this.touchPoints.push(touchPoint) } } }
绘制手势锁
for (let i = 0; i < this.touchPoints.length; i++){ this.drawCircle(this.touchPoints[i].x, this.touchPoints[i].y, this.radius, this.initColor) } this.ctx.draw(true);
接下来就是识别用户的滑动行为,判断用户划过了哪些圆圈,进而识别出用户的手势。
在touchstart和touchmove事件中检测触发并更新画布
onTouchStart(e) { // 不识别多点触控 if (e.touches.length > 1){ this.touchState = "unTouch"; return; } this.touchState = "startTouch"; this.checkTouch(e); let point = {x:e.touches[0].x, y:e.touches[0].y}; this.drawCanvas(this.checkColor, point); } onTouchMove(e) { if (e.touchState === "unTouch") { return; } if (e.touches.length > 1){ this.touchState = "unTouch"; return; } this.checkTouch(e); let point = {x:e.touches[0].x, y:e.touches[0].y}; this.drawCanvas(this.checkColor, point); }
检测用户是否划过某个圆圈
checkTouch(e) { for (let i = 0; i < this.touchPoints.length; i++){ let point = this.touchPoints[i]; if (isPointInCycle(e.touches[0].x, e.touches[0].y, point.x, point.y, this.radius)) { if (point.check === 'uncheck') { this.checkPoints.push(point); this.lastCheckPoint = point; } point.check = "check" return; } } }
更新画布
drawCanvas(color, point) { //每次更新之前先清空画布 this.ctx.clearRect(0, 0, this.width, this.height); //使用不同颜色和形式绘制已触发和未触发的锁 for (let i = 0; i < this.touchPoints.length; i++){ let point = this.touchPoints[i]; if (point.check === "check") { this.drawCircle(point.x, point.y, this.radius, color); this.drawCircleCentre(point.x, point.y, color); } else { this.drawCircle(this.touchPoints[i].x, this.touchPoints[i].y, this.radius, this.initColor) } } //绘制已识别锁之间的线段 if (this.checkPoints.length > 1) { let lastPoint = this.checkPoints[0]; for (let i = 1; i < this.checkPoints.length; i++) { this.drawLine(lastPoint, this.checkPoints[i], color); lastPoint = this.checkPoints[i]; } } //绘制最后一个识别锁和当前触摸点之间的线段 if (this.lastCheckPoint && point) { this.drawLine(this.lastCheckPoint, point, color); } this.ctx.draw(true); }
当用户滑动结束时调用回调函数并传递识别出的手势
onTouchEnd(e) { typeof this.onEnd === 'function' && this.onEnd(this.checkPoints, false); } onTouchCancel(e) { typeof this.onEnd === 'function' && this.onEnd(this.checkPoints, true); }
重置和显示手势错误
gestureError() { this.drawCanvas(this.errorColor) } reset() { for (let i = 0; i < this.touchPoints.length; i++) { this.touchPoints[i].check = 'uncheck'; } this.checkPoints = []; this.lastCheckPoint = null; this.drawCanvas(this.initColor); }
如何调用
在onload方法中创建lock对象并在用户触摸事件中调用相应方法
onLoad: function () { var s = this; this.lock = new Lock("id-gesture-lock", wx.createCanvasContext("id-gesture-lock"), function(checkPoints, isCancel) { console.log('over'); s.lock.gestureError(); setTimeout(function() { s.lock.reset(); }, 1000); }, {width:300, height:300}) this.lock.drawGestureLock(); console.log('onLoad') var that = this //调用应用实例的方法获取全局数据 app.getUserInfo(function(userInfo){ //更新数据 that.setData({ userInfo:userInfo }) that.update() }) }, onTouchStart: function (e) { this.lock.onTouchStart(e); }, onTouchMove: function (e) { this.lock.onTouchMove(e); }, onTouchEnd: function (e) { this.lock.onTouchEnd(e); }
以上就是本文的全部内容,希望对大家的学习有所帮助,更多相关内容请关注PHP中文网!
相关推荐:
以上是微信小程序开发之手势解锁的介绍的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

如何在iOS17中使用iPhone上的FaceTime效果【注】FaceTime通话效果仅适用于iPhone12及更高机型。打开FaceTime通话App,选取联系人,然后进行视频通话。连接后,请确保已启用前置摄像头。有两种方法可以在iOS17上的FaceTime中触发效果。首先,在FaceTime中长按您的图片,这应该会显示屏幕上的FaceTime效果菜单,如心形,竖起大拇指,烟花等。点击效果以查看动画。在iOS17上的FaceTime中触发反应效果的第二种也是更令人兴奋的方法是解放双手并使用

Windows11提供了一些新的强大功能,这些功能也很容易定制。因此,根据您的需要设置它们将创建一个更愉快和原始的数字环境。其中,触摸板和触摸屏功能非常流行,两者都需要特别注意。因为我们希望始终让您了解最新信息,所以在今天的文章中,我们将探讨如何在Windows11中个性化触控板手势。但在此之前,让我们先看看支持哪些手势以及为什么要使用它们。Windows11支持哪些触控板手势?点击手势点击和滑动代表两种常见的手势,主要用于笔记本电脑或平板电脑等设备。点击手势用于检测一个或多个手指短暂按下触

检查自定义触摸板手势支持如果您想使用自定义触摸板手势,则需要确保您有合适的驱动程序来支持它们。以下是您需要做的检查:检查触摸板驱动程序。如果您没有精密触摸板,则“设置”应用将不允许您在Windows11上自定义触摸板手势。如果您有像BrydgeTrackpad之类的,请确保更新其驱动程序。安装第三方软件。如果触控板有第三方软件,结果将取决于软件的强大程度和应用程序个性化的能力。如何在Windows11上自定义触摸板手势Windows11包含适用于带有WindowsPrecision

随着移动互联网技术和智能手机的普及,微信成为了人们生活中不可或缺的一个应用。而微信小程序则让人们可以在不需要下载安装应用的情况下,直接使用小程序来解决一些简单的需求。本文将介绍如何使用Python来开发微信小程序。一、准备工作在使用Python开发微信小程序之前,需要安装相关的Python库。这里推荐使用wxpy和itchat这两个库。wxpy是一个微信机器

小程序能用react,其使用方法:1、基于“react-reconciler”实现一个渲染器,生成一个DSL;2、创建一个小程序组件,去解析和渲染DSL;3、安装npm,并执行开发者工具中的构建npm;4、在自己的页面中引入包,再利用api即可完成开发。

实现微信小程序中的卡片翻转特效在微信小程序中,实现卡片翻转特效是一种常见的动画效果,可以提升用户体验和界面交互的吸引力。下面将具体介绍如何在微信小程序中实现卡片翻转的特效,并提供相关代码示例。首先,需要在小程序的页面布局文件中定义两个卡片元素,一个用于显示正面内容,一个用于显示背面内容,具体示例代码如下:<!--index.wxml-->&l

本站10月31日消息,今年5月27日,蚂蚁集团宣布启动“汉字拾光计划”,最近又迎来新进展:支付宝上线“汉字拾光-生僻字”小程序,用于向社会征集生僻字,补充生僻字库,同时提供不同的生僻字输入体验,以帮助完善支付宝内的生僻字输入方法。目前,用户搜索“汉字拾光”、“生僻字”等关键词就可以进入“生僻字”小程序。在小程序里,用户可以提交尚未被系统识别录入的生僻字图片,支付宝工程师在确认后,将会对字库进行补录入。本站注意到,用户还可以在小程序体验最新的拆字输入法,这一输入法针对读音不明确的生僻字设计。用户拆

uniapp如何实现小程序和H5的快速转换,需要具体代码示例近年来,随着移动互联网的发展和智能手机的普及,小程序和H5成为了不可或缺的应用形式。而uniapp作为一个跨平台的开发框架,可以在一套代码的基础上,快速实现小程序和H5的转换,大大提高了开发效率。本文将介绍uniapp如何实现小程序和H5的快速转换,并给出具体的代码示例。一、uniapp简介unia
