垂直拖动(touchMove)钛合金appcelerator视图

时间:2017-02-12 14:19:50

标签: titanium appcelerator appcelerator-titanium appcelerator-mobile

我想垂直拖动钛金属加速器中的视图,并在屏幕上显示整个视图高度时停止。 在开始时,视图的高度为140,底部为-120,因此我可以看到一块可以开始拖动的视图:

Here is the screenshot

这就是我想垂直拖动" myView",x轴保持为0所以我只能从下到上拖动它,目的是在显示所有myView高度时停止拖动(所有myView都可见)

var WIDTH = (OS_ANDROID) ? Ti.Platform.displayCaps.platformWidth / dpi : Ti.Platform.displayCaps.platformWidth;
var HEIGHT = (OS_ANDROID) ? Ti.Platform.displayCaps.platformHeight / dpi : Ti.Platform.displayCaps.platformHeight;
var sx = 0;
var sy = 0;
var cx = 0;
var cy = 0;
var xDistance = 0;
function onTouchStart(e) {
    // start movement
    sx = e.x;
    sy = e.y;
   cx = e.x;
   cy = e.y;
   console.log(e.x);
}

function onTouchMove(e) {
    xDistance = cx - sx;
    var yDistance = cy - sy;

    var points = e.source.convertPointToView({
        x: e.x,
        y: e.y
    }, $.single);


$.myView.applyProperties({
        left: 0,
        top: points.y,
        opacity: 1
    });

/*------------------------------------------------------
* HERE I TRY TO DETECT IF myView HEIGHT IS SHOWN ENTIRELY
* is myView.top + myView.height > viewport ?
------------------------------------------------------*/
var t = $.myView.top + $.myView.height;
if( t >= HEIGHT ){
    alert('all myView is visible now ???');
    return; // <= why this has no effect ??
}

cx = e.x;
cy = e.y;

}
function onTouchEnd(e) {
  // check xDistance
}

$.myView.addEventListener("touchmove", onTouchMove);
$.myView.addEventListener("touchstart", onTouchStart);
$.myView.addEventListener("touchend", onTouchEnd);

使用此代码,我可以垂直拖动myView,但是当完全显示此高度时,它不会停止。

我的第一个问题是:   - 如果完全显示myView,我该怎么说?   - 如果显示所有myView高度,如何禁用垂直拖动到顶部?

谢谢

1 个答案:

答案 0 :(得分:0)

onTouchMove函数应该是这样的。

function onTouchMove(e) {

    /*------------------------------------------------------
    * HERE I TRY TO DETECT IF myView HEIGHT IS SHOWN ENTIRELY
    * is myView.top + myView.height > viewport ?
    ------------------------------------------------------*/
    var t = $.myView.top + $.myView.height;
    if( t >= HEIGHT ){
        alert('all myView is visible now ???');
        return; // <= why this has no effect ??
    }

    xDistance = cx - sx;
    var yDistance = cy - sy;

    var points = e.source.convertPointToView({
        x: e.x,
        y: e.y
    }, $.single);


    $.myView.applyProperties({
        left: 0,
        top: points.y,
        opacity: 1
    });

    cx = e.x;
    cy = e.y;

}