国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

首頁 微信小程序 小程序開發(fā) 微信小程序開發(fā)之手勢解鎖的介紹

微信小程序開發(fā)之手勢解鎖的介紹

Jun 22, 2018 pm 05:29 PM
小程序 手勢 手勢密碼

手勢解鎖是app上常見的解鎖方式,相比輸入密碼方式操作起來要方便許多。這篇文章主要介紹了微信小程序開發(fā)教程-手勢解鎖實例,有興趣的可以了解一下。

手勢解鎖是app上常見的解鎖方式,相比輸入密碼方式操作起來要方便許多。下面展示如何基于微信小程序?qū)崿F(xiàn)手機(jī)解鎖。最終實現(xiàn)效果如下圖:

整個功能基于canvas實現(xiàn),首先添加畫布組件,并設(shè)定樣式

<!--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;
}

手勢解鎖實現(xiàn)代碼在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; //觸摸點(diǎn)半徑
    this.isParamOk = false;
    this.marge = this.margeCircle = 25; //觸摸點(diǎn)及觸摸點(diǎn)和畫布邊界間隔
    this.initColor = opt && opt.initColor || &#39;#C5C5C3&#39;;  
    this.checkColor = opt && opt.checkColor || &#39;#5AA9EC&#39;;
    this.errorColor = opt && opt.errorColor || &#39;#e19984&#39;;
    this.touchState = "unTouch";
    this.checkParam();
    this.lastCheckPoint = null;
    if (this.isParamOk) {
      // 計算觸摸點(diǎn)的半徑長度
      this.radius = (this.width - this.marge * 2 - (this.margeCircle * (this.cycleNum - 1))) / (this.cycleNum * 2)
      this.radius = Math.floor(this.radius);
      // 計算每個觸摸點(diǎn)的圓心位置
      this.calCircleParams();
    }
    this.onEnd = cb; //滑動手勢結(jié)束時的回調(diào)函數(shù)
  }

主要設(shè)置一些參數(shù),如canvas的長寬,canvas的context,手勢鎖的個數(shù)(3乘3, 4乘4),手勢鎖的顏色,手勢滑動結(jié)束時的回調(diào)函數(shù)等。并計算出手勢鎖的半徑。

計算每個手勢鎖的圓心位置

  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);

接下來就是識別用戶的滑動行為,判斷用戶劃過了哪些圓圈,進(jìn)而識別出用戶的手勢。

在touchstart和touchmove事件中檢測觸發(fā)并更新畫布

  onTouchStart(e) {
    // 不識別多點(diǎn)觸控
    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 === &#39;uncheck&#39;) {
          this.checkPoints.push(point);
          this.lastCheckPoint = point;
        }
        point.check = "check"
        return;
      }
    }
  }

更新畫布

   drawCanvas(color, point) {
    //每次更新之前先清空畫布
    this.ctx.clearRect(0, 0, this.width, this.height);
    //使用不同顏色和形式繪制已觸發(fā)和未觸發(fā)的鎖
    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];
       }
    }
    //繪制最后一個識別鎖和當(dāng)前觸摸點(diǎn)之間的線段
    if (this.lastCheckPoint && point) {
      this.drawLine(this.lastCheckPoint, point, color);
    }
    this.ctx.draw(true);
  }

當(dāng)用戶滑動結(jié)束時調(diào)用回調(diào)函數(shù)并傳遞識別出的手勢

  onTouchEnd(e) {
    typeof this.onEnd === &#39;function&#39; && this.onEnd(this.checkPoints, false);
  }

  onTouchCancel(e) {
    typeof this.onEnd === &#39;function&#39; && this.onEnd(this.checkPoints, true);
  }

重置和顯示手勢錯誤

  gestureError() {
    this.drawCanvas(this.errorColor)
  }

  reset() {
    for (let i = 0; i < this.touchPoints.length; i++) {
      this.touchPoints[i].check = &#39;uncheck&#39;;
    }
    this.checkPoints = [];
    this.lastCheckPoint = null;
    this.drawCanvas(this.initColor);
  }

如何調(diào)用

在onload方法中創(chuàng)建lock對象并在用戶觸摸事件中調(diào)用相應(yīng)方法

 onLoad: function () {
  var s = this;
  this.lock = new Lock("id-gesture-lock", wx.createCanvasContext("id-gesture-lock"), function(checkPoints, isCancel) {
   console.log(&#39;over&#39;);
   s.lock.gestureError();
   setTimeout(function() {
    s.lock.reset();
   }, 1000);
  }, {width:300, height:300})
  this.lock.drawGestureLock();
  console.log(&#39;onLoad&#39;)
  var that = this
  //調(diào)用應(yīng)用實例的方法獲取全局?jǐn)?shù)據(jù)
  app.getUserInfo(function(userInfo){
   //更新數(shù)據(jù)
   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);
 }

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,更多相關(guān)內(nèi)容請關(guān)注PHP中文網(wǎng)!

相關(guān)推薦:

微信小程序之小豆瓣圖書的介紹

微信小程序列表的上拉加載和下拉刷新的實現(xiàn)

以上是微信小程序開發(fā)之手勢解鎖的介紹的詳細(xì)內(nèi)容。更多信息請關(guān)注PHP中文網(wǎng)其他相關(guān)文章!

本站聲明
本文內(nèi)容由網(wǎng)友自發(fā)貢獻(xiàn),版權(quán)歸原作者所有,本站不承擔(dān)相應(yīng)法律責(zé)任。如您發(fā)現(xiàn)有涉嫌抄襲侵權(quán)的內(nèi)容,請聯(lián)系admin@php.cn

熱AI工具

Undress AI Tool

Undress AI Tool

免費(fèi)脫衣服圖片

Undresser.AI Undress

Undresser.AI Undress

人工智能驅(qū)動的應(yīng)用程序,用于創(chuàng)建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用于從照片中去除衣服的在線人工智能工具。

Clothoff.io

Clothoff.io

AI脫衣機(jī)

Video Face Swap

Video Face Swap

使用我們完全免費(fèi)的人工智能換臉工具輕松在任何視頻中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費(fèi)的代碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

功能強(qiáng)大的PHP集成開發(fā)環(huán)境

Dreamweaver CS6

Dreamweaver CS6

視覺化網(wǎng)頁開發(fā)工具

SublimeText3 Mac版

SublimeText3 Mac版

神級代碼編輯軟件(SublimeText3)

如何在iOS 17上的FaceTime中使用效果 如何在iOS 17上的FaceTime中使用效果 Sep 23, 2023 pm 04:53 PM

如何在iOS17中使用iPhone上的FaceTime效果【注】FaceTime通話效果僅適用于iPhone12及更高機(jī)型。打開FaceTime通話App,選取聯(lián)系人,然后進(jìn)行視頻通話。連接后,請確保已啟用前置攝像頭。有兩種方法可以在iOS17上的FaceTime中觸發(fā)效果。首先,在FaceTime中長按您的圖片,這應(yīng)該會顯示屏幕上的FaceTime效果菜單,如心形,豎起大拇指,煙花等。點(diǎn)擊效果以查看動畫。在iOS17上的FaceTime中觸發(fā)反應(yīng)效果的第二種也是更令人興奮的方法是解放雙手并使用

使用Python開發(fā)微信小程序 使用Python開發(fā)微信小程序 Jun 17, 2023 pm 06:34 PM

隨著移動互聯(lián)網(wǎng)技術(shù)和智能手機(jī)的普及,微信成為了人們生活中不可或缺的一個應(yīng)用。而微信小程序則讓人們可以在不需要下載安裝應(yīng)用的情況下,直接使用小程序來解決一些簡單的需求。本文將介紹如何使用Python來開發(fā)微信小程序。一、準(zhǔn)備工作在使用Python開發(fā)微信小程序之前,需要安裝相關(guān)的Python庫。這里推薦使用wxpy和itchat這兩個庫。wxpy是一個微信機(jī)器

實現(xiàn)微信小程序中的卡片翻轉(zhuǎn)特效 實現(xiàn)微信小程序中的卡片翻轉(zhuǎn)特效 Nov 21, 2023 am 10:55 AM

實現(xiàn)微信小程序中的卡片翻轉(zhuǎn)特效在微信小程序中,實現(xiàn)卡片翻轉(zhuǎn)特效是一種常見的動畫效果,可以提升用戶體驗和界面交互的吸引力。下面將具體介紹如何在微信小程序中實現(xiàn)卡片翻轉(zhuǎn)的特效,并提供相關(guān)代碼示例。首先,需要在小程序的頁面布局文件中定義兩個卡片元素,一個用于顯示正面內(nèi)容,一個用于顯示背面內(nèi)容,具體示例代碼如下:&lt;!--index.wxml--&gt;&l

如何在 Windows 11 上自定義觸摸板手勢 如何在 Windows 11 上自定義觸摸板手勢 Apr 15, 2023 pm 04:55 PM

檢查自定義觸摸板手勢支持如果您想使用自定義觸摸板手勢,則需要確保您有合適的驅(qū)動程序來支持它們。以下是您需要做的檢查:檢查觸摸板驅(qū)動程序。如果您沒有精密觸摸板,則“設(shè)置”應(yīng)用將不允許您在Windows11上自定義觸摸板手勢。如果您有像BrydgeTrackpad之類的,請確保更新其驅(qū)動程序。安裝第三方軟件。如果觸控板有第三方軟件,結(jié)果將取決于軟件的強(qiáng)大程度和應(yīng)用程序個性化的能力。如何在Windows11上自定義觸摸板手勢Windows11包含適用于帶有WindowsPrecision

Windows 11的觸控板手勢設(shè)置方法是什么? Windows 11的觸控板手勢設(shè)置方法是什么? May 09, 2023 am 09:22 AM

Windows11提供了一些新的強(qiáng)大功能,這些功能也很容易定制。因此,根據(jù)您的需要設(shè)置它們將創(chuàng)建一個更愉快和原始的數(shù)字環(huán)境。其中,觸摸板和觸摸屏功能非常流行,兩者都需要特別注意。因為我們希望始終讓您了解最新信息,所以在今天的文章中,我們將探討如何在Windows11中個性化觸控板手勢。但在此之前,讓我們先看看支持哪些手勢以及為什么要使用它們。Windows11支持哪些觸控板手勢?點(diǎn)擊手勢點(diǎn)擊和滑動代表兩種常見的手勢,主要用于筆記本電腦或平板電腦等設(shè)備。點(diǎn)擊手勢用于檢測一個或多個手指短暫按下觸

支付寶上線'漢字拾光-生僻字”小程序,用于征集、補(bǔ)充生僻字庫 支付寶上線'漢字拾光-生僻字”小程序,用于征集、補(bǔ)充生僻字庫 Oct 31, 2023 pm 09:25 PM

本站10月31日消息,今年5月27日,螞蟻集團(tuán)宣布啟動“漢字拾光計劃”,最近又迎來新進(jìn)展:支付寶上線“漢字拾光-生僻字”小程序,用于向社會征集生僻字,補(bǔ)充生僻字庫,同時提供不同的生僻字輸入體驗,以幫助完善支付寶內(nèi)的生僻字輸入方法。目前,用戶搜索“漢字拾光”、“生僻字”等關(guān)鍵詞就可以進(jìn)入“生僻字”小程序。在小程序里,用戶可以提交尚未被系統(tǒng)識別錄入的生僻字圖片,支付寶工程師在確認(rèn)后,將會對字庫進(jìn)行補(bǔ)錄入。本站注意到,用戶還可以在小程序體驗最新的拆字輸入法,這一輸入法針對讀音不明確的生僻字設(shè)計。用戶拆

小程序能用react嗎 小程序能用react嗎 Dec 29, 2022 am 11:06 AM

小程序能用react,其使用方法:1、基于“react-reconciler”實現(xiàn)一個渲染器,生成一個DSL;2、創(chuàng)建一個小程序組件,去解析和渲染DSL;3、安裝npm,并執(zhí)行開發(fā)者工具中的構(gòu)建npm;4、在自己的頁面中引入包,再利用api即可完成開發(fā)。

uniapp如何實現(xiàn)小程序和H5的快速轉(zhuǎn)換 uniapp如何實現(xiàn)小程序和H5的快速轉(zhuǎn)換 Oct 20, 2023 pm 02:12 PM

uniapp如何實現(xiàn)小程序和H5的快速轉(zhuǎn)換,需要具體代碼示例近年來,隨著移動互聯(lián)網(wǎng)的發(fā)展和智能手機(jī)的普及,小程序和H5成為了不可或缺的應(yīng)用形式。而uniapp作為一個跨平臺的開發(fā)框架,可以在一套代碼的基礎(chǔ)上,快速實現(xiàn)小程序和H5的轉(zhuǎn)換,大大提高了開發(fā)效率。本文將介紹uniapp如何實現(xiàn)小程序和H5的快速轉(zhuǎn)換,并給出具體的代碼示例。一、uniapp簡介unia

See all articles