Time for action – showing the accelerometer on a slider
To show the accelerometer on a slider, perform the following steps:
Create a new blank mobile app by clicking on File | New | Titanium Project. Don't use a template as it will just generate code that gets in the way.
Create the window for the app:
var win1 = Titanium.UI.createWindow({ backgroundColor:'#fff' });
Create the slider that will display the accelerometer pitch value. Also add a label to show the raw value. Add both of these to the window:
var masterVw = Ti.UI.createView({layout: 'vertical'}); var rawSlider = Ti.UI.createSlider({max: 2 ,min:-2}); var rawLabel = Titanium.UI.createLabel({}); layout.add(rawSlider); layout.add(rawLabel); win1.add(masterVw);
Create a function to update the slider and label it with the updated accelerometer value. Notice that the function computes the pitch by using the tangent of the
z
andy
values:updateSliders = function(e) { var raw = Math.atan(e.z/e...