舟山闪爸电子技术有限公司

小程序的拖拽、縮放和旋轉(zhuǎn)手勢(shì)功能的開發(fā)

在開發(fā)中,有時(shí)會(huì)遇到像App中的手勢(shì)那樣的效果,那么小程序要做出類似的效果,需要怎么開發(fā)?

小程序的拖拽、縮放和旋轉(zhuǎn)手勢(shì)功能的開發(fā)

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

小程序的拖拽、縮放和旋轉(zhuǎn)手勢(shì)功能的開發(fā)


【本站聲明】
  1、本站文章中所選用的圖片及文字來源于網(wǎng)絡(luò)以及用戶投稿,由于未聯(lián)系到知識(shí)產(chǎn)權(quán)人或未發(fā)現(xiàn)有關(guān)知識(shí)產(chǎn)權(quán)的登記,如有知識(shí)產(chǎn)權(quán)人并不愿意我們使用,如果有侵權(quán)請(qǐng)立即聯(lián)系。
  2、本網(wǎng)站不對(duì)文章中所涉及的內(nèi)容真實(shí)性、準(zhǔn)確性、可靠性負(fù)責(zé),僅系客觀性描述,如您需要了解該類商品/服務(wù)詳細(xì)的資訊,請(qǐng)您直接與該類商品/服務(wù)的提供者聯(lián)系。


KESION 科汛軟件

KESION 科汛軟件是國內(nèi)領(lǐng)先的在線教育軟件及私域社交電商軟件服務(wù)提供商,長(zhǎng)期專注于為企業(yè)提供在線教育軟件及社交電商SaaS平臺(tái)解決方案。
公司核心產(chǎn)品云開店SaaS社交電商服務(wù)平臺(tái)、在線教育SaaS服務(wù)平臺(tái)、教育企業(yè)數(shù)字化SaaS云平臺(tái)、企微營銷助手、私有化獨(dú)立部署品牌網(wǎng)校和在線教育咨詢等。

KESION 不斷通過技術(shù)創(chuàng)新,提供產(chǎn)品和服務(wù),助力企業(yè)向數(shù)字化轉(zhuǎn)型,通過科技驅(qū)動(dòng)商業(yè)革新,讓商業(yè)變得更智慧!



▼點(diǎn)擊進(jìn)入科汛官網(wǎng)了解更多



熱門標(biāo)簽
SaaS
上/下篇
換一換相關(guān)推薦
精選內(nèi)容
熱點(diǎn)精選