2012-11-12 201 views
0

在学习Appcelerator Titanium时,我正在构建一个以包含2个标签的Window开始的应用程序。两个标签(onclick)应该打开2个不同的Windows(每个包含tabgroups)。钛设计建议

所以,在我app.js我:

Window = require('ui/handheld/ApplicationWindow'); 
ApplicationWindow功能

和:

var window1 = Ti.UI.createWindow({ 
     backgroundColor:'#ffffff' 
    }); 

    var label = Ti.UI.createLabel({ text: "Open first Window", top:10 }); 
    window1.add(label); 

    var label2 = Ti.UI.createLabel({ text: "Open second Window", top:50 }); 
    window1.add(label2); 

    var window2 = Titanium.UI.createWindow({url:"A.js",backgroundColor:'#00ff00'}); 
    var window3 = Titanium.UI.createWindow({url:"B.js",backgroundColor:'#ff0000'}); 

    label.addEventListener("click", function(e) { 
    var t = Ti.UI.iPhone.AnimationStyle.CURL_UP; 
    window1.animate({view:window2,transition:t},function(){window2.open();}); 
    }); 

    label2.addEventListener("click", function(e) { 
    var t = Ti.UI.iPhone.AnimationStyle.CURL_UP; 
    window1.animate({view:window3,transition:t},function(){window3.open();}); 
    }); 

    return window1; 

第一个问题是:这是一个好的设计?它可以改进吗?怎么样?

第二个问题是:有没有办法显示我在转换结束前打开的页面?目前看来,A.js和B.js中包含的JS仅在动画停止时执行。

谢谢,任何帮助,欢迎和遗憾的新手问题。

[编辑]这是Ch4rAss的评论后,我当前的代码:

function ApplicationWindow() { 

    var root = Ti.UI.createWindow({ 
     backgroundColor:'#ffffff' 
    }); 

    var label = Ti.UI.createLabel({ text: "Open first Window", top:10 }); 
    root.add(label); 

    var label2 = Ti.UI.createLabel({ text: "Open second Window", top:50 }); 
    root.add(label2); 

    var win2 = require('ui/handheld/Win2'); 
    var win3 = require('ui/handheld/Win3'); 

    label.addEventListener("click", function(e) { 
    var t = Ti.UI.iPhone.AnimationStyle.CURL_UP; 
    win2.open({transition:t}); 
    }); 

    label2.addEventListener("click", function(e) { 
    var t = Ti.UI.iPhone.AnimationStyle.CURL_UP; 
    win3.open({transition:t}); 
    }); 

    return root; 
} 

module.exports = ApplicationWindow; 

和:

function Win2(){ 
     /* You can (of course) do more than this */ 
     return Ti.UI.createWindow({backgroundColor:'#00ff00'}); 
    } 
module.exports = Win2; 

回答

2

你应该在动画中,为了打开窗口中添加动画open()方法:

window1.open({transition:Titanium.UI.iPhone.AnimationStyle.CURL_UP}); 

我会建议为每个窗口使用单独的文件a nd使用CommonJS到require()吧。您使用的方法与ApplicationWindow相同。在你创建两个窗口并将它们加载到内存中而不使用它们的时刻!更好的方式是:

var Win1 = require('ui/handheld/WhateverWindow'); 
label.addEventListener("click", function(e) { 
    var win1 = new Win1(); 
    win1.open({transition:Titanium.UI.iPhone.AnimationStyle.CURL_UP}); 
}); 

而且ui/handheld/WhateverWindow.js

(function() { 
    var WhateverWindow = function() { 
     /* You can (of course) do more than this */ 
     return Ti.UI.createWindow({backgroundColor:'#00ff00'}); 
    } 
    module exports = WhateverWindow; 
})(); 

你可以阅读更多here

+0

这不适合我。我改了一点你的代码,W2中的函数W2变成了:'function Win2(){return Ti.UI.createWindow({backgroundColor:'#00ff00'});} module.exports = Win2;'。调用代码与您的代码很相似,但是当我点击时我收到一个错误。我的代码:'var win2 = require('ui/handheld/W2'); label.addEventListener(“click”,function(e){var t = Ti.UI.iPhone.AnimationStyle.CURL_UP; win2.open({transition :t});});'错误:行= 30; message =“'undefined'不是函数(评估'win2.open({transition:t})')”; name = TypeError; sourceId = 208669216; – ContentiousMaximus

+0

我编辑了问题主体以显示我当前的代码! – ContentiousMaximus

+0

也许我是未知的,但我等待我们的评论:我必须以这种方式实例化赢:var Win2 = require('ui/handheld/Win2'); var win2 = new Win2()。然后,win.open(...)... – ContentiousMaximus