Created
April 19, 2013 17:11
-
-
Save jabubuck/5421737 to your computer and use it in GitHub Desktop.
Titanium code sample that uses pickers to randomly select 3 values and display them side by side. iOS only(for now). You can do whatever you like with this code.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var win1 = Titanium.UI.createWindow({ | |
backgroundColor:'black', | |
layout: 'vertical', | |
fullscreen: true, | |
width: Ti.Platform.displayCaps.platformWidth, | |
height: Ti.Platform.displayCaps.platformHeight | |
}); | |
//Create window that will contain the picker | |
var pickerView = Ti.UI.createView({ | |
backgroundColor:'white', | |
width: Ti.Platform.displayCaps.platformWidth, | |
height: Ti.UI.SIZE | |
}); | |
win1.add(pickerView); | |
var picker = Ti.UI.createPicker({selectionIndicator: true, useSpinner:true}); | |
var columnArr = []; | |
//Add the Titles of the picker rows | |
var rows = ['Pedro','Shak','Mauro','Eduardo','Jamie','Daniel','Betty','Davide','Carter']; | |
//Systematically add all the values to the picker | |
for (i = 0; i < 3; i++){ | |
var column = Ti.UI.createPickerColumn(); | |
for(x = 0; x < rows.length; x++){ | |
column.addRow(Ti.UI.createPickerRow({title: rows[x]})); | |
}; | |
columnArr.push(column); | |
}; | |
picker.add(columnArr); | |
pickerView.add(picker); | |
var uiView = Ti.UI.createView({ | |
backgroundColor:'grey', | |
width: Ti.Platform.displayCaps.platformWidth, | |
height: Ti.UI.FILL | |
}); | |
win1.add(uiView); | |
var spinButton = Ti.UI.createButton({ | |
title: 'SPIN!', | |
height: Ti.Platform.displayCaps.platformHeight * 0.3, | |
width: Ti.Platform.displayCaps.platformWidth * 0.9 | |
}); | |
//On click spin the pickers up and down and then randomly with delays between. | |
spinButton.addEventListener('click', function(){ | |
spinButton.touchEnabled = 'false'; | |
setTimeout(spinDown, 500); | |
setTimeout(spinUp, 1000); | |
setTimeout(spinDown, 1500); | |
setTimeout(spin, 2000); | |
setTimeout( spinButton.touchEnabled = 'true', 2500); | |
}); | |
//Spin the picker to the first positions | |
function spinUp(){ | |
picker.setSelectedRow(0, 0, true); | |
picker.setSelectedRow(1, 0, true); | |
picker.setSelectedRow(2, 0, true); | |
}; | |
//Spin the picker to the last positions | |
function spinDown(){ | |
picker.setSelectedRow(0, 8, true); | |
picker.setSelectedRow(1, 8, true); | |
picker.setSelectedRow(2, 8, true); | |
}; | |
//Spin the picker to random positions | |
function spin(){ | |
picker.setSelectedRow(0, Math.floor(Math.random()*9), true), | |
picker.setSelectedRow(1, Math.floor(Math.random()*9), true); | |
picker.setSelectedRow(2, Math.floor(Math.random()*9), true); | |
}; | |
uiView.add(spinButton); | |
win1.open(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment