ios - 像卡片一样翻转钛合金 View

标签 ios titanium

我正在使用 Titanium 并在 iOS7 上运行模拟器。我正在尝试获得一张类似翻转动画的卡片。我已经工作一半了。当前它会从 front 翻转到 back,但是一旦我再次单击它,应用程序就会崩溃,并且我的控制台不会显示任何内容。

文档说:

要转换到的新 View 不应是另一个 View 或动画 View 的 subview 。

也许我的理解是错误的,因为我似乎无法完成这项工作。到目前为止我的代码是:

var win = Ti.UI.currentWindow;
var noThumbColors   = ['#555555','#cccccc'];
var noThumbColors2  = ['#ff0000','#000'];

var views   = [];
var fronts  = [];
var backs   = [];
for (var i = 0; i < 1; i++)
{   
    fronts[i] = Ti.UI.createView({
        id:i,
        name:"front",
        width:150,
        height:150,
        backgroundGradient:{
            type: 'linear',
            startPoint: { x: '0%', y: '0%' },
            endPoint: { x: '0%', y: '100%' },
            colors: [ { color: noThumbColors[0], offset: 0.0}, { color: noThumbColors[1], offset: 1.0 } ],
        },
        currentAngle: 10
    });

    backs[i] = Ti.UI.createView({
        id:i,
        name:"back",
        width:150,
        height:150,
        backgroundGradient:{
            type: 'linear',
            startPoint: { x: '0%', y: '0%' },
            endPoint: { x: '0%', y: '100%' },
            colors: [ { color: noThumbColors2[1], offset: 0.0}, { color: noThumbColors2[0], offset: 1.0 } ],
        },
        currentAngle: 10
    });
    win.add(backs[i]);
    win.add(fronts[i]);
    fronts[i].addEventListener('click', function(e)
    {
        log(e.source.name);
        t = Ti.UI.iPhone.AnimationStyle.FLIP_FROM_LEFT;
        e.source.animate({view:backs[e.source.id],transition:t});

    });

    backs[i].addEventListener('click', function(e)
    {
        log(e.source.name);
        t = Ti.UI.iPhone.AnimationStyle.FLIP_FROM_RIGHT;
        e.source.animate({view:fronts[e.source.id],transition:t});
    });
}

function log(msg)
{
    Ti.API.info(msg);
}

完成的工作代码(将生成 3 个独立来回翻转的方 block ):

var win = Ti.UI.currentWindow;
var noThumbColors   = ['#555555','#cccccc'];
var noThumbColors2  = ['#ff0000','#000'];
var containers  = [];
var fronts      = [];
var backs       = [];

for (var i = 0; i < 3; i++)
{   
    containers[i] = Ti.UI.createView({
        top:50 + (i * 155),
        width:150,
        height:150
    });

    fronts[i] = Ti.UI.createView({
        id:i,
        name:"front",
        width:150,
        height:150,
        backgroundGradient:{
            type: 'linear',
            startPoint: { x: '0%', y: '0%' },
            endPoint: { x: '0%', y: '100%' },
            colors: [ { color: noThumbColors[0], offset: 0.0}, { color: noThumbColors[1], offset: 1.0 } ],
        },
        currentAngle: 10
    });

    backs[i] = Ti.UI.createView({
        id:i,
        name:"back",
        width:150,
        height:150,
        backgroundGradient:{
            type: 'linear',
            startPoint: { x: '0%', y: '0%' },
            endPoint: { x: '0%', y: '100%' },
            colors: [ { color: noThumbColors2[1], offset: 0.0}, { color: noThumbColors2[0], offset: 1.0 } ],
        },
        currentAngle: 10
    });
    containers[i].add(backs[i]);
    containers[i].add(fronts[i]);
    win.add(containers[i]);
}

win.addEventListener('click', function(e)
{
    if (e.source.name === "front")
    {
        containers[e.source.id].animate({view:backs[e.source.id],transition:Ti.UI.iPhone.AnimationStyle.FLIP_FROM_LEFT});
    }
    else if (e.source.name === "back")
    {
        containers[e.source.id].animate({view:fronts[e.source.id],transition:Ti.UI.iPhone.AnimationStyle.FLIP_FROM_RIGHT});
    }
});

function log(msg)
{
    Ti.API.info(msg);
}

最佳答案

基于KitchenSink example app您应该对要设置动画的 View 的父对象调用 animate。我还进行了小型重构,并仅创建了一个附加到窗口的事件监听器,而不是为您创建的每个对象创建了许多函数。很好用JSHint ,这会告诉您在 for 循环中创建函数并不是最佳实践。

for (var i = 0; i < 1; i++) { 
    /* your code */
    win.add(backs[i]);
    win.add(fronts[i]);
   /* both addEventListener() are removed from here */
}

win.addEventListener('click', function(e) {
    var t;
    if (e.source.name === 'front') {
        t = Ti.UI.iPhone.AnimationStyle.FLIP_FROM_LEFT;
        win.animate({view:backs[e.source.id],transition:t});
    } else {
        t = Ti.UI.iPhone.AnimationStyle.FLIP_FROM_RIGHT;
        win.animate({view:fronts[e.source.id],transition:t});
    }
});

关于ios - 像卡片一样翻转钛合金 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19917100/

相关文章:

android - Titanium Android 标签栏文字颜色

ios - XIB 弹出窗口显示

iphone - 识别按下选项卡按钮以移动到下一个响应者

ios - UITableViewCell 的 accessoryView 中的多个图像

android - 为 Phonegap/Titanium 跨平台开发选择硬件

javascript - Titanium HealthKit 模块 - 无法创建按来源累积和分隔的统计查询

ios - 如何确定使用 hyperloop 的 cocoapod 的正确 require 语句是什么?

ios - 在 UIWebView 中获取页面标题?

php - Apple Production Push Notification 证书在新应用版本后无法连接

javascript - 在 Titanium 桌面应用程序中编辑默认菜单