我正在构建一个跳跃运动控制的音乐播放器,菜单系统通过跳跃运动进行控制。
向上和向下滚动菜单项,向右滑动选择该项,向左滑动返回菜单级别。
我遇到的问题是当您向右滑动“艺术家列表”时。菜单,然后更改为歌曲'菜单,因为跳跃动作滑动手势在帧上工作,它会多次触发我的功能,所以第一首歌就会被立即选中。
以下是相关代码:
var updateSelection = function() {
if(Math.floor(x) == x && $.isNumeric(x)){
var listLength = $(menu + ' li').size();
if (x > listLength) x = listLength;
if (x < 1) x = 1;
$(menu + ' li').removeClass('active');
$(menu + ' #' + x + artist ).addClass('active');
scrollToIt = x + artist;
idTagX = x;
if (scrollDirection == "down") {
var topPos = document.getElementById(scrollToIt).offsetTop;
document.getElementById('cssmenu').scrollTop = topPos;
//document.getElementById(scrollToIt).scrollIntoView();
}
else if (scrollDirection == "up"){
var topPos = document.getElementById(scrollToIt).offsetTop;
document.getElementById('cssmenu').scrollTop = topPos;
//document.getElementById(scrollToIt).scrollIntoView(true);
}
}
}
if(frame.hands.length > 0)
{
var hand = frame.hands[0];
pitchRadians = hand.pitch();
if (pitchRadians > 0.45) {
newX = x - 0.1;
x = roundNumber(newX, 2);
scrollDirection = "up";
updateSelection();
}
else if (pitchRadians < -0.45){
newX = x + 0.1;
x = roundNumber(newX, 2);
scrollDirection = "down";
updateSelection();
}
else {
x = parseInt(x);
updateSelection();
}
}
//--------------------------------------------------Swipe Selection
if (frame.gestures.length > 0) {
for (var i = 0; i < frame.gestures.length; i++) {
var gesture = frame.gestures[i];
//---- Swipe ----
if (gesture.type == "swipe") {
var isHorizontal = Math.abs(gesture.direction[0]) > Math.abs(gesture.direction[1]);
if(isHorizontal){
if(gesture.direction[0] > 0){
$(menu + ' #' + idTagX + artist).click();
console.log(artist);
} else {
console.log("Menu Left");
// $("#" + artist).hide();
//menu = ".artists"
}
} else {
// Vertical swipe
}
}
}
}
})
这可能是一个简单的解决方法,我刚刚看了这段代码太久了,无法理解它。
答案 0 :(得分:2)
我已经为此编写了一个解决方案,详情如下,回答以便其他用户可以查看以供将来参考。
var prevSwipe = "";
var d = new Date();
var now = d.getTime();
var prevSwipeTime = now;
function rightSwipe () {
d = new Date();
if(prevSwipe == "right") {
if(prevSwipeTime + 1000 < d.getTime()) { //time elapsed, run code
$(menu + ' #' + idTagX + artist).click();
prevSwipe = "right";
prevSwipeTime = d.getTime();
console.log("prev == right");
} // otherwise do nothing
} else {
$(menu + ' #' + idTagX + artist).click();
prevSwipe = "right";
prevSwipeTime = d.getTime();
console.log(prevSwipeTime);
console.log("prev != right");
}
}
希望这有助于某人!