以下示例代码将简单地完成您的工作。这里我使用了按钮而不是 imageView。您可以使用它更改您的代码。
var win = Ti.UI.createWindow({
backgroundColor : 'white'
});
var currentView = Ti.UI.createView({
backgroundColor : '#EFEFEF'
});
var button = [],top = 0;
for (var i = 0; i < 5; i++){
top += 80;
//Creating each button
button[i] = Titanium.UI.createButton({
color : 'red',
top : top,
width : '80%',
value : 1
});
button[i].title = 'State ' + button[i].value;
button[i].addEventListener('click',changeState);
//Adding the buttons to the center view
currentView.add(button[i]);
}
var buttonState = Titanium.UI.createButton({
color : 'red',
top : top + 80,
title : 'View button states',
width : '80%',
});
var lblStates = Titanium.UI.createLabel({
color : 'red',
layout: 'horizontal',
top : top + 160,
text : 'Click on show button to view the button states',
width : '80%',
});
buttonState.addEventListener('click', showButtonStates);
currentView.add(lblStates);
currentView.add(buttonState);
win.add(currentView);
win.open();
//Changing the state of the clicked button
function changeState(e){
e.source.value= 2;
e.source.title = 'State ' + e.source.value;
for(var i = 0;i<5;i++){
if(e.source !== button[i]){
button[i].value = 1;
button[i].title = 'State ' + button[i].value;
}
}
}
//To display the button state
function showButtonStates(){
lblStates.text = "";
for(var i =0;i<5;i++){
lblStates.text = lblStates.text + '\nbutton' + (i+1) + ' ---> state: ' + button[i].value;
}
}