我正在使用 Titanium 并在 iOS7 上运行模拟器。我正在尝试获得类似翻转动画的卡片。我已经成功了一半。它当前会从 翻转front
到back
,但是一旦我再次单击它,应用程序就会崩溃,并且我的控制台没有显示任何内容。
文档说:
The new view being transitioned to should NOT be a child of another view or of the animating view.
也许我理解错了,因为我似乎无法完成这项工作。到目前为止,我的代码是:
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 个独立来回翻转的方块):
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);
}