微信小程序-类似探探的卡片拖拽切换

效果

用户可以通过拖拽卡片,来将其移除。除此之外还添加了大量的交互式动画。

 

效果图

实现

为了提升运行速度,采用了只生成两张卡片,并将其不断复用的方法。

当用户点击卡片式,记录此次的点击位置,并将这张卡片变小,产生按压的效果。

 

  viewTouchInside: function (event) {

    var that = this

    var pointX = event.touches[0].clientX
    var pointY = event.touches[0].clientY

    that.setData({
      startX: pointX,
      startY: pointY
    })

    var animation = wx.createAnimation({
      duration: 100,
      timingFunction: 'ease-out',
    })
    animation.scale(0.9).step()

    if (that.data.isFirstCard == true) {
      that.setData({
        animation1: animation.export()
      })
    } else {
      that.setData({
        animation2: animation.export()
      })
    }
  }

当用户拖拽卡片时,卡片会随着手指移动,并旋转。下方的卡片也会逐渐从透明度为0的状态显现出来。

 

viewDidMove: function (event) {
    
    var that = this

    var pointX = event.touches[0].clientX
    var pointY = event.touches[0].clientY

    var widthCenter = that.data.windowWidth / 2
    var heightCenter = that.data.windowHeight / 2

    var perX = (pointX - that.data.startX) / widthCenter
    var perY = (pointY - that.data.startY) / heightCenter
    var maxPer = (Math.abs(perX) > Math.abs(perY)) ? Math.abs(perX) : Math.abs(perY)


    var animationOpacity = wx.createAnimation({
      duration: 100,
      timingFunction: 'ease-out',
    })
    animationOpacity.opacity(maxPer).step()

    var animationRotate = wx.createAnimation({
      duration: 100,
      timingFunction: 'ease-out',
    })
    animationRotate.scale(0.9).rotate(perX * 20).step()

    var x = marginHori + pointX - that.data.startX
    var y = marginVerti + pointY - that.data.startY

    if (that.data.isFirstCard == true) {
      that.setData({
        left1: x,
        top1: y
      })
      that.setData({
        animation1: animationRotate.export(),
        animation2: animationOpacity.export()
      })
    } else {
      that.setData({
        left2: x,
        top2: y
      })
      that.setData({
        animation1: animationOpacity.export(),
        animation2: animationRotate.export(),
      })
    }
  }

当用户手指离开卡片时,判断是否要进行移除卡片操作。如果需要,则根据卡片的方位移除该卡片,否则将卡片回归原位。

 

viewTouchUpDownInside: function (event) {

    var that = this

    var endX = event.changedTouches[0].clientX
    var endY = event.changedTouches[0].clientY

    var distanceX = endX - that.data.startX
    var distanceY = endY - that.data.startY

    if (distanceX > 93.75) {
      that.removeCard('right')
    } else if (distanceX < -93.75) {
      that.removeCard('left')
    } else if (distanceY < -100) {
      that.removeCard('up')
    } else if (distanceY > 100) {
      that.removeCard('down')
    }

    if (distanceX < 93.75 && distanceX > -93.75 && distanceY > -150 && distanceY < 150) {
      if (that.data.isFirstCard == true) {
        that.setData({
          top1: marginVerti,
          left1: marginHori
        })
      } else {
        that.setData({
          top2: marginVerti,
          left2: marginHori
        })
      }
    }

    var animation = wx.createAnimation({
      duration: 100,
      timingFunction: 'ease-out',
    })
    animation.scale(1).step()

    if (that.data.isFirstCard == true) {
      that.setData({
        animation1: animation.export()
      })
    } else {
      that.setData({
        animation2: animation.export()
      })
    }

    if (that.data.data.length - that.data.count < 5) {
      that.requestMenu()
    }
  }

以下是移除卡片操作,首先先将卡片移除屏幕,随后再将数据重新填充,回归原位,实现复用机制。

 

removeCard: function (direction) {

    var that = this

    var animation = wx.createAnimation({
      duration: 250,
      timingFunction: 'linear',
    })

    if (direction == 'right') {
      animation.translateX(400).rotate(45).opacity(0).step()
      animation.translateX(0).rotate(0).step()
    } else if (direction == 'left') {
      animation.translateX(-400).rotate(-45).opacity(0).step()
      animation.translateX(0).rotate(0).step()
    } else if (direction == 'up') {
      animation.translateY(-400).opacity(0).step()
      animation.translateY(0).step()
    } else if (direction == 'down') {
      animation.translateY(400).opacity(0).step()
      animation.translateY(0).step()
    }
    
    if (that.data.isFirstCard == true) {
      that.setData({
        animation1: animation.export(),
        count1: that.data.count1 + 2,
        count: that.data.count + 1,
        name1: that.data.data[that.data.count1 + 2]["name"],
        isLike1: that.data.data[that.data.count1 + 2]["is_like"],
        location1: that.data.data[that.data.count1 + 2]["location"],
        isFirstCard: false
      })

      setTimeout(function () {
        that.setData({
          top1: marginVerti,
          left1: marginHori
        })
      }.bind(that), 250)
    } else {
      that.setData({
        animation2: animation.export(),
        count2: that.data.count2 + 2,
        count: that.data.count + 1,
        name2: that.data.data[that.data.count2 + 2]["name"],
        isLike2: that.data.data[that.data.count2 + 2]["is_like"],
        location2: that.data.data[that.data.count2 + 2]["location"],
        isFirstCard: true
      })

      setTimeout(function () {
        that.setData({
          top2: marginVerti,
          left2: marginHori
        })
      }.bind(that), 100)

      that.setImgURL()
    }
  },

源码

作者


 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
无论你是一个程序猿还是一个程序媛,每天干的事除了coding还是coding,代码不能解决的问题(什么问题自己心里还没点abcd数嘛),探探能帮你解决。最近网上特流行一款交友软件叫探探(据说是yp软件)。作为探探曾经的一名从来只浏览图片但是没有yue过的资深玩家同时又是一位热爱前端的妹子,我决定要仿一下这个app。既然是寄几开发,那还不是寄几说了算,毫无疑问整款APP的主题风格被我改成我最爱的终极少女粉了hhh,下面让我们一起来感受下探探的魅力吧~项目整体效果项目部分功能点解析主页图片左滑右滑对应按钮变化首先我们来聊一下最让我头痛的地方,就是主页面的左右滑动事件并且对应按钮会相应变化,即我左滑一下图片下面的灰色按钮会有相应的动画效果,右滑则对应着图片下面的红色按钮。对于一个刚入小程序坑的妹子来说,没有大神指点不知道要在这里面的逻辑坑还要绕多久才能绕出来。得一高人指点,我才完美滴实现了这个功能。这里写了三个大的盒子放着图片和文字信息,再将他们放到swiper-item里面,用swiper组件实现整个盒子的左右滑动                                          K             ♂21             金牛座             文化/教育                哦盒子下面不是按钮,我是放了两张图片。             先给他们写个滑动的时候触发的动画效果.active {    animation: active 1s ease;//定义一个叫做active的动画} @keyframes active {//补充active动作脚本     0% {        transform: scale(0.8);     }     50% {        transform: scale(1.2);     }     100% {        transform: scale(1.0);     } }在page的data里面定义三个变量,将left,right变量绑定到对应图片中data: {        left: false ,       right: false,        activeIndex: 0 },在swiper绑定事件中具体判断左右滑动事件changeswiper: function(e) {         var index = e.detail.current;//当前所在页面的 index     if(index > this.data.activeIndex) {//左滑事件判断       this.setData({         left: true//若为左滑,left值为true,触发图片动画效果       })     } else if(index  {//每滑动一次,数据发生变化       this.setData({         activeIndex: index,         left:false,         right:false       })     }, 1000);   },从本地上传图片这个呀查一查小程序开发文档就好了,先在要上传图片的地方的src绑定个数据变量放入图片默认地址,就是上传图片之前的添加图片data: {     imgUrl: '../../images/addImg.png'   },通过绑定tap事件将上传的图片地址替换进去uploadImg: function(e) { var that = this; wx.chooseImage({   count: 1, //上传图片数量   sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有   sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有   success: function (res) {// 返回选定照片的本地文件路径列表,tempFilePath可以作为img标签的src属性显示图片       var tempFilePaths = res.tempFilePaths;       that.setData({           imgUrl: tempFilePaths     })       wx.showToast({//显示上传成功           title: '上传成功',           icon: 'success',           duration: 2000     })   } }),配对成功列表据通过easy-mock获取后台数据block wx:for渲染一个包含多节点的结构块                                                                                                                         {{item.nickname}}                     {{item.message}}                                            获取后台数据wx.request({       url: 'https://www.easy-mock.com/mock/5a23dbf382614c0dc1bebf04/getFriendsList/getFriendsList',       success: (res) => {         // console.log(response);         this.setData({           friendsList: res.data.data.friendsList         })       }     })其它差不多就是切页面了,个人原因用不太习惯weui的官方样式,每个页面都是我自己呕心沥血码出来的,所以大家不喜轻点喷哈,还在努力学习当中~~~项目开发用到的一些工具微信开发者工具、VScode、GithubIconfont阿里巴巴矢量图标库:各种图片logo应有尽有,前端开发必备esay-mock:模拟数据请求,实现无后端编程W3Cschool微信小程序开发教程手册文档:开发小程序要多看看哦小结emmmm目前项目功能还是很简单呀,还有很多功能后面慢慢实现吧~比如利用将上传的图片放到storage中,页面刷新之后图片依然在,slider滑动到某一处在页面上保存当前值,模拟配对成功后弹出提醒页面等等......也希望遇到热爱学习的小伙伴一起交流学习,一起在前端坑里越陷越深hhh项目地址:https://github.com/beautifulg... 求鼓励~求star呀~我的邮箱:804316947@qq.com 这里可以找到我哦作者:略略略
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值