效果
用户可以通过拖拽卡片,来将其移除。除此之外还添加了大量的交互式动画。
效果图
实现
为了提升运行速度,采用了只生成两张卡片,并将其不断复用的方法。
当用户点击卡片式,记录此次的点击位置,并将这张卡片变小,产生按压的效果。
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()
}
},
源码
作者