2

我正在使用 Titanium SDK 3.1.1 并部署到 Android 4.0 设备。我正在尝试向任何方向拖动视图,但是在触发 touchmove 事件时,出现问题并且事件被中断。只要我尝试水平拖动touchmove 事件就会起作用,如果我尝试在y轴上拖动视图,则事件会中断。应用程序不会崩溃,也不会触发 touchend 事件。在查看 logcat 时,我得到以下信息:

D/InputEventConsistencyVerifier(11897): TouchEvent: ACTION_MOVE contained 1 pointers but there are currently 0 pointers down.
D/InputEventConsistencyVerifier(11897):   in android.view.ScaleGestureDetector@41223570
D/InputEventConsistencyVerifier(11897):   0: sent at 199051208557000, MotionEvent { action=ACTION_MOVE, id[0]=0, x[0]=214.55301, y[0]=163.79526, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=1, eventTime=199051208, downTime=199051042, deviceId=2, source=0x1002 }
D/InputEventConsistencyVerifier(11897):   -- recent events --
D/InputEventConsistencyVerifier(11897):   1: sent at 199050732492000, MotionEvent { action=ACTION_UP, id[0]=0, x[0]=186.61124, y[0]=73.907616, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x80000000, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=199050732, downTime=199049818, deviceId=2, source=0x1002 }
D/InputEventConsistencyVerifier(11897):   2: sent at 199050713565000, MotionEvent { action=ACTION_MOVE, id[0]=0, x[0]=186.61124, y[0]=73.907616, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x80000000, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=199050713, downTime=199049818, deviceId=2, source=0x1002 }
D/InputEventConsistencyVerifier(11897):   3: sent at 199050693173000, MotionEvent { action=ACTION_MOVE, id[0]=0, x[0]=205.57172, y[0]=81.89763, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x80000000, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=199050693, downTime=199049818, deviceId=2, source=0x1002 }
D/InputEventConsistencyVerifier(11897):   4: sent at 199050683204000, MotionEvent { action=ACTION_MOVE, id[0]=0, x[0]=233.51352, y[0]=96.87891, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x80000000, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=199050683, downTime=199049818, deviceId=2, source=0x1002 }
D/InputEventConsistencyVerifier(11897):   5: sent at 199050669151000, MotionEvent { action=ACTION_MOVE, id[0]=0, x[0]=264.44907, y[0]=115.85519, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x80000000, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=199050669, downTime=199049818, deviceId=2, source=0x1002 }

我不明白为什么会发生这种情况,我之前在 Android 中使用 Titanium 做过拖放行为并且没有遇到这个问题。这是 touchstart、touchmove 和 touchend 事件的代码:

var draggedView = Titanium.UI.createView({
        backgroundColor : 'transparent',
        width : circleWidth + 'dp',
        widthNumber : circleWidth,
        height : circleWidth + 'dp',
        heightNumber : circleWidth,
        left : '10dp',
        top : '10dp',
        leftNumber : 10,
        topNumber : 10,
        zIndex : 4,
    });
    var dragView = Titanium.UI.createView({
        backgroundColor : 'transparent',
        opacity : 1,
        width : circleWidth + 'dp',
        height : circleWidth + 'dp',
        widthNumber : circleWidth,
        heightNumber : circleWidth,
        left : '10dp',
        top : '20dp',
        leftNumber : 10,
        topNumber : 20,
        zIndex : 4
    });
    var targetView = Ti.UI.createImageView({
        image : imagesPath + "KineduPaperPlane.png",
        width : '100dp',
        widthNumber : 100,
        height : '100dp',
        heightNumber : 100,
        bottom : '-120dp',
        bottomNumber : -120
    });
    dragView.addEventListener('touchstart', function(e) {
        if (dragEnabled == true) {
            curX = e.x;
            curY = e.y;
        }
    });

    dragView.addEventListener('touchmove', function(e) {
        if (dragEnabled == true) {
            if (targetViewDisplayed == false) {
                targetView.animate({
                    bottom : '20dp',
                    duration : 200
                });
                targetView.bottomNumber = 20;
                targetViewDisplayed = true;
            }

            deltaX = e.x - curX;
            deltaY = e.y - curY;
            currentPositionX = initialViewX + deltaX;
            currentPositionY = initialViewY + deltaY;

            draggedView.setLeft(currentPositionX + 'dp');
            draggedView.leftNumber = currentPositionX;
            draggedView.setTop(currentPositionY + 'dp');
            draggedView.topNumber = currentPositionY;

        }
    });

    function recallControl() {
        var animateControlToOrigin = Ti.UI.createAnimation({
            left : initialViewX,
            top : initialViewY,
            duration : 300,
        });
        animateControlToOrigin.addEventListener("complete", function(e) {

            draggedView.setLeft(initialViewX + 'dp');
            draggedView.leftNumber = initialViewX;
            draggedView.setTop(initialViewY + 'dp');
            draggedView.topNumber = initialViewY;

            dragView.setLeft(initialViewX + 'dp');
            dragView.leftNumber = initialViewX;
            dragView.setTop(initialViewY + 'dp');
            dragView.topNumber = initialViewY;

        });

        draggedView.animate(animateControlToOrigin);
    }

    dragView.addEventListener("touchend", function(e) {
        if (dragEnabled == true) {

            if (targetViewDisplayed == true) {
                var leftLimit = true;
                var rightLimit = true;
                var topLimit = false;
                var lowerLimit = false;

                if (currentPositionY > (Ti.Platform.displayCaps.platformHeight - circleWidth - 100)) {
                    topLimit = true;
                }

                if (currentPositionY < Ti.Platform.displayCaps.platformHeight) {
                    lowerLimit = true;
                }

                if (leftLimit && rightLimit && topLimit && lowerLimit) {
                    var circleWidthAnimated = 20;

                    var animationLeft = (Ti.Platform.displayCaps.platformWidth / 2) - (circleWidth / 2);
                    var draggedViewAwayAnimation = Ti.UI.createAnimation({
                        left : animationLeft + 'dp',
                        height : circleWidthAnimated + 'dp',
                        width : circleWidthAnimated + 'dp',
                        borderRadius : circleWidthAnimated / 2,
                        opacity : 0,
                        duration : 400
                    })
                    draggedView.animate(draggedViewAwayAnimation);

                    var targetViewAwayAnimationPart2 = Ti.UI.createAnimation({
                        top : '10dp',
                        left : '-150dp',
                        duration : 1200,
                        opacity : 0,
                    });
                    draggedViewAwayAnimation.addEventListener("complete", function(e) {
                        targetView.animate(targetViewAwayAnimationPart1);
                        targetView.leftNumber = animationLeft;
                        targetView.heightNumber = circleWidthAnimated;
                        targetView.widthNumber = circleWidthAnimated;
                    });

                    var animationLeftPart1 = (Ti.Platform.displayCaps.platformWidth / 2) - circleWidthAnimated - (2) + 30;
                    var targetViewAwayAnimationPart1 = Ti.UI.createAnimation({
                        bottom : 0,
                        left : animationLeftPart1 + 'dp',
                        duration : 600,
                    });

                    targetViewAwayAnimationPart1.addEventListener("complete", function(e) {
                        targetView.animate(targetViewAwayAnimationPart2);
                        targetView.bottomNumber = 0;
                        targetView.leftNumber = animationLeftPart1;
                    });

                    targetViewAwayAnimationPart2.addEventListener("complete", function(e) {
                        dragEnabled = false;
                        targetView.leftNumber = -150;
                        targetView.top = 10;
                    });

                } else {

                    targetView.animate({
                        bottom : '-20dp',
                        duration : 200
                    });
                    targetView.bottomNumber = -20;
                    targetViewDisplayed = false;

                    recallControl();
                }
            }
        }
    });

dragView 是我用来检测触摸事件的透明视图,而 draggedView 是被这些事件上检测到的更改移动的视图。targetView 是 draggedView 的最终目的地。

由于我使用 dpi 显示视图,因此我添加了一些属性,其中包含诸如 top、left、width 等属性的数值。如果有办法使用 dpi 和算术运算,我会很高兴被告知如何去做.

回到我的问题,我不明白 touchmove 事件停止的原因。该消息ACTION_MOVE contained 1 pointers but there are currently 0 pointers down给我的印象是,在我尝试垂直拖动视图时手指跟踪丢失了。

有人可以直接解决手头的问题吗?是什么导致了这种行为?感谢您提前提供任何帮助。

4

1 回答 1

1

did targetView stack on any view with horizontal scroll behavior?

i had the same issue before, mine is because i have a scrollview nested inside a scrollableview. i solve it via switching the scrollingEnabled to be true once at a time only, other to be false.

so i make a guess, it might because one of the view in the stack u have include inside the window have an horizontal scrolling enabled and causes when u scroll horizontally, the pointer doesnt know which view's event should goes to. check from window to targetView.

just a guess, hope it helps!

于 2013-09-09T08:11:49.363 回答