溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

小程序的拖拽、縮放和旋轉(zhuǎn)手勢(shì)功能怎么實(shí)現(xiàn)

發(fā)布時(shí)間:2022-03-15 10:03:13 來(lái)源:億速云 閱讀:530 作者:iii 欄目:開(kāi)發(fā)技術(shù)

這篇文章主要介紹“小程序的拖拽、縮放和旋轉(zhuǎn)手勢(shì)功能怎么實(shí)現(xiàn)”,在日常操作中,相信很多人在小程序的拖拽、縮放和旋轉(zhuǎn)手勢(shì)功能怎么實(shí)現(xiàn)問(wèn)題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”小程序的拖拽、縮放和旋轉(zhuǎn)手勢(shì)功能怎么實(shí)現(xiàn)”的疑惑有所幫助!接下來(lái),請(qǐng)跟著小編一起來(lái)學(xué)習(xí)吧!

小程序的拖拽、縮放和旋轉(zhuǎn)手勢(shì)功能怎么實(shí)現(xiàn)

wxml部分:

<view class="touch-container">
 <view class="msg">{{msg}}</view>
 <image
 class="img"
 src="{{src}}"
 style="width: {{width}}rpx; height: {{height}}rpx; left: {{left}}rpx; top: {{top}}rpx; transform: translate(-50%, -50%) scale({{ scale }}) rotate({{ rotate }}deg);"
 bindload="bindload"
 catchtouchstart="touchstart"
 catchtouchmove="touchmove"
 catchtouchend="touchend"
 ></image>
</view>

wxss部分:

page {
  width: 100%;
  height: 100%;
  background: #ffffff;
}
.touch-container {
  width: 100%;
  height: 100%;
  padding-top: 0.1px;
}
.msg {
  width: 100%;
  height: 60rpx;
  line-height: 60rpx;
  text-align: center;
  font-size: 30rpx;
  color: #666666;
}
.img {
  position: absolute;
  width: 690rpx;
  height: 300rpx;
  transform-origin: center center;
}

js部分:

var canOnePointMove = false
var onePoint = {
  x: 0,
  y: 0
}
var twoPoint = {
  x1: 0,
  y1: 0,
  x2: 0,
  y2: 0
}
Page({
  data: {
    msg: '',
    src: 'http://img01.taopic.com/150508/318763-15050PU9398.jpg',
    width: 0,
    height: 0,
    left: 375,
    top: 600,
    scale: 1,
    rotate: 0
 },
 // 關(guān)閉上拉加載
  onReachBottom: function() {
 return
 },
  bindload: function(e) {
 var that = this
 var width = e.detail.width
 var height = e.detail.height
 if (width > 750) {
      height = 750 * height / width
      width = 750
 }
 if (height > 1200) {
      width = 1200 * width / height
      height = 1200
 }
    that.setData({
      width: width,
      height: height
 })
 },
  touchstart: function(e) {
 var that = this
 if (e.touches.length < 2) {
      canOnePointMove = true
      onePoint.x = e.touches[0].pageX * 2
      onePoint.y = e.touches[0].pageY * 2
 }else {
      twoPoint.x1 = e.touches[0].pageX * 2
      twoPoint.y1 = e.touches[0].pageY * 2
      twoPoint.x2 = e.touches[1].pageX * 2
      twoPoint.y2 = e.touches[1].pageY * 2
 }
 },
  touchmove: function(e){
 var that = this
 if (e.touches.length < 2 && canOnePointMove) {
 var onePointDiffX = e.touches[0].pageX * 2 - onePoint.x
 var onePointDiffY = e.touches[0].pageY * 2 - onePoint.y
      that.setData({
        msg: '單點(diǎn)移動(dòng)',
        left: that.data.left + onePointDiffX,
        top: that.data.top + onePointDiffY
 })
      onePoint.x = e.touches[0].pageX * 2
      onePoint.y = e.touches[0].pageY * 2
 }else if (e.touches.length > 1) {
 var preTwoPoint = JSON.parse(JSON.stringify(twoPoint))
      twoPoint.x1 = e.touches[0].pageX * 2
      twoPoint.y1 = e.touches[0].pageY * 2
      twoPoint.x2 = e.touches[1].pageX * 2
      twoPoint.y2 = e.touches[1].pageY * 2
 // 計(jì)算角度,旋轉(zhuǎn)(優(yōu)先)
 var perAngle = Math.atan((preTwoPoint.y1 - preTwoPoint.y2)/(preTwoPoint.x1 - preTwoPoint.x2))*180/Math.PI
 var curAngle = Math.atan((twoPoint.y1 - twoPoint.y2)/(twoPoint.x1 - twoPoint.x2))*180/Math.PI
 if (Math.abs(perAngle - curAngle) > 1) {
        that.setData({
          msg: '旋轉(zhuǎn)',
          rotate: that.data.rotate + (curAngle - perAngle)
 })
 }else {
 // 計(jì)算距離,縮放
 var preDistance = Math.sqrt(Math.pow((preTwoPoint.x1 - preTwoPoint.x2), 2) + Math.pow((preTwoPoint.y1 - preTwoPoint.y2), 2))
 var curDistance = Math.sqrt(Math.pow((twoPoint.x1 - twoPoint.x2), 2) + Math.pow((twoPoint.y1 - twoPoint.y2), 2))
        that.setData({
          msg: '縮放',
          scale: that.data.scale + (curDistance - preDistance) * 0.005
 })
 }
 }
 },
  touchend: function(e) {
 var that = this
    canOnePointMove = false
 }
})

json部分:

"navigationBarTitleText": "識(shí)別手勢(shì)",
 "navigationBarTextStyle":"black",
 "navigationBarBackgroundColor": "#FFF",
 "disableScroll": true

到此,關(guān)于“小程序的拖拽、縮放和旋轉(zhuǎn)手勢(shì)功能怎么實(shí)現(xiàn)”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注億速云網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)?lái)更多實(shí)用的文章!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI