Tinder喜欢功能,appcelerator

时间:2016-04-25 00:17:47

标签: javascript appcelerator appcelerator-titanium tinder

我尝试重新创建一个类似Tinder的功能。

我找到了这段代码:

var win = Titanium.UI.createWindow({
    backgroundColor: "#ffffff",
    title: "win"
});

// animations
var animateLeft = Ti.UI.createAnimation({
    left: -520,
    transform: Ti.UI.create2DMatrix({rotate: 60}),
    opacity: 0,
    duration: 300
});
var animateRight = Ti.UI.createAnimation({
    left: 520,
    transform: Ti.UI.create2DMatrix({rotate: -60}),
    opacity: 0,
    duration: 300
});

var curX = 0;

win.addEventListener('touchstart', function (e) {
    curX = parseInt(e.x, 10);
});

win.addEventListener('touchmove', function (e) {
    if (!e.source.id || e.source.id !== 'oferta') {
        return;
    }

    // Subtracting current position to starting horizontal position
    var coordinates = parseInt(e.x, 10) - curX;
    // Defining coordinates as the final left position

    var matrix = Ti.UI.create2DMatrix({rotate: -(coordinates / 10)});

    var animate = Ti.UI.createAnimation({
        left: coordinates,
        transform: matrix,
        duration: 20
    });

    e.source.animate(animate);

    e.source.left = coordinates;
});

win.addEventListener('touchend', function (e) {
    if (!e.source.id || e.source.id !== 'oferta') {
        return;
    }

    // No longer moving the window
    if (e.source.left >= 75) {
        e.source.animate(animateRight);
    } else if (e.source.left <= -75) {
        e.source.animate(animateLeft);
    } else {
        // Repositioning the window to the left
        e.source.animate({
            left: 0,
            transform: Ti.UI.create2DMatrix({rotate: 0}),
            duration: 300
        });
    }
});

for (var i = 0; i < 10; i++) {

    var wrap = Ti.UI.createView({
        "id": 'oferta',
        "width": 320,
        "height": 400,
        "backgroundColor": (i % 2 == 0 ? "red" : "blue")
    });

    var text = Ti.UI.createLabel({
        text: "row: " + i,
        color: "black"
    });

    wrap.add(text);

    win.add(wrap);
}

win.open();

但是有一种奇怪的行为。 事实上,当我从顶部拍摄环绕视图时,每个都可以,但如果我将手指放在环绕视图的底部,图像会变得疯狂..

尝试代码,你会看到奇怪的行为。

我使用的是Titanium SDK 5.2.2 和iPhone 6上的iOS 9.3.1。

这是一段显示奇怪事情的视频:http://tinypic.com/player.php?v=x37d5u%3E&s=9#.Vx_zDaOLQb0

(对不起视频大小) 谢谢你的帮助

3 个答案:

答案 0 :(得分:1)

使用此代码转换pxToDp,反之亦然:

将以下代码放在lib文件夹中并包含它 有要求(“测量”)  而不是要求(“合金/测量”)

var dpi = Ti.Platform.displayCaps.dpi, density = Ti.Platform.displayCaps.density;

exports.dpToPX = function(val) {
    switch (density) {
      case "xxxhigh":
        return 5 * val;

      case "xxhigh":
        return 4 * val;

      case "xhigh":
        return 3 * val;

      case "high":
        return 2 * val;

      default:
        return val;
    }
};

exports.pxToDP = function(val) {
    switch (density) {
      case "xxxhigh":
        return 5 / val;

      case "xxhigh":
        return 4 / val;
      case "xhigh":
        return val / 3;

      case "high":
        return val / 2;

      default:
        return val;
    }
};

exports.pointPXToDP = function(pt) {
    return {
        x: exports.pxToDP(pt.x),
        y: exports.pxToDP(pt.y)
    };
};

答案 1 :(得分:1)

非常感谢所有人!它使用此代码::

var win = Titanium.UI.createWindow({
    backgroundColor: "#ffffff",
    title: "win"
});

// animations
var animateLeft = Ti.UI.createAnimation({
    left: -520,
    transform: Ti.UI.create2DMatrix({rotate: 60}),
    opacity: 0,
    duration: 300
});
var animateRight = Ti.UI.createAnimation({
    left: 520,
    transform: Ti.UI.create2DMatrix({rotate: -60}),
    opacity: 0,
    duration: 300
});

Ti.include('measurement.js');


var curX = 0;
var wrap = [];
var topWrap = 100; //(Titanium.Platform.displayCaps.platformHeight - 400) / 2;
var leftWrap =  50; //(Titanium.Platform.displayCaps.platformWidth - 320) / 2;


for (var i = 0; i < 10; i++) {

    wrap[i] = Ti.UI.createView({
    "id": 'oferta',
    "width": Titanium.Platform.displayCaps.platformWidth - 100,
    "height": Titanium.Platform.displayCaps.platformHeight - 300,
    image:(i % 2 == 0 ? 'principale.png' : 'principale1.png'),
    "backgroundColor": (i % 2 == 0 ? "red" : "blue"),
    top:topWrap,
    left:leftWrap,
});



    wrap[i].addEventListener('touchstart', function (e) {
//        curX = parseInt(e.x, 10);
          curX = pxToDP(parseInt(e.x, 10));
//          curY = pxToDP(parseInt(e.Y, 10));
    });

    wrap[i].addEventListener('touchmove', function (e) {

        // Subtracting current position to starting horizontal position
//        var coordinates = parseInt(e.x, 10) - curX;
        // Defining coordinates as the final left position
var coordinatesX = pxToDP(parseInt(e.x, 10))  - curX;
//var coordinatesY = pxToDP(parseInt(e.y, 10))  - curY;
        var matrix = Ti.UI.create2DMatrix({rotate: -(coordinatesX / 10)});


        var animate = Ti.UI.createAnimation({
            left: coordinatesX,
//            top: coordinatesY,
            transform: matrix,
            duration: 10
        });

        e.source.animate(animate);

        e.source.left = coordinatesX;
//        e.source.top = coordinatesY;

    });

    wrap[i].addEventListener('touchend', function (e) {

        // No longer moving the window
        if (e.source.left >= 75) {
            e.source.animate(animateRight);
        } else if (e.source.left <= -75) {
            e.source.animate(animateLeft);
        } else {
            // Repositioning the window to the left
            e.source.animate({
                left: leftWrap,
                transform: Ti.UI.create2DMatrix({rotate: 0}),
                duration: 300
            });
        }
    });






    win.add(wrap);
}

win.open();

measurement.js文件是:

var dpi = Ti.Platform.displayCaps.dpi, density = Ti.Platform.displayCaps.density;

function dpToPX(val) {
    switch (density) {
      case "xxxhigh":
        return 5 * val;

      case "xxhigh":
        return 4 * val;

      case "xhigh":
        return 3 * val;

      case "high":
        return 2 * val;

      default:
        return val;
    }
};

function pxToDP(val) {
    switch (density) {
      case "xxxhigh":
        return 5 / val;

      case "xxhigh":
        return 4 / val;
      case "xhigh":
        return val / 3;

      case "high":
        return val / 2;

      default:
        return val;
    }
};

function pointPXToD(pt) {
    return {
        x: pxToDP(pt.x),
        y: pxToDP(pt.y)
    };
};

答案 2 :(得分:0)

您必须将px转换为dp。

var measurement = require('alloy/measurement');

win.addEventListener('touchstart', function (e) {
  curX =  measurement.pxToDP(parseInt(e.x, 10));
  Ti.API.info("touchstart curX: " + curX);
});

...
win.addEventListener('touchmove', function (e) {
if (!e.source.id || e.source.id !== 'oferta') {
    return;
}

// Subtracting current position to starting horizontal position
var coordinates = measurement.pxToDP(parseInt(e.x, 10))  - curX;
...
相关问题