无法在Android中垂直拖动视图

时间:2013-08-01 16:02:50

标签: android titanium appcelerator titanium-mobile touchmove

我正在使用Titanium SDK 3.1.1并部署到Android 4.0设备。我正试图在任意方向中拖动一个视图,但是当触摸事件被触发时,出现问题并且事件被中断。只要我尝试水平拖动,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 }

我不明白为什么会这样,我已经完成了拖拉和放大使用Titanium在Android之前删除行为并且没有遇到此问题。 这是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给我的印象是,在我尝试垂直拖动视图时手指跟踪丢失了。

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

1 个答案:

答案 0 :(得分:1)

在具有水平滚动行为的任何视图上执行了targetView堆栈?

我之前遇到过同样的问题,因为我的滚动视图嵌套在一个可滚动的视图中。我通过仅将 scrollingEnabled 切换为true来解决此问题,其他方面都是假的。

所以我做了一个猜测,可能是因为堆栈中的一个视图你已经在窗口中包含了一个水平滚动启用并导致当你水平滚动时,指针不知道哪个视图的事件应该去。检查从窗口到targetView。

只是一个猜测,希望它有所帮助!