【原文】[url=https://wiki.appcelerator.org/display/guides/Handling+Device+Orientation]Handling Device Orientation[/url]
[size=large][b]Run-time Device Orientation Detection[/b][/size]
You can detect the current device orientation by checking value of the Ti.UI.orientation property. This value will match one of the orientation constants defined under the Ti.UI namespace:
[list]
[*]Ti.UI.PORTRAIT
[*]Ti.UI.UPSIDE_PORTRAIT
[*]Ti.UI.LANDSCAPE_LEFT
[*]Ti.UI.LANDSCAPE_RIGHT
[/list]
An example use of this property might be to define helper functions to determine if the device is in a landscape or portrait orientation:
[size=large][b]Handling Orientation Changes[/b][/size]
Orientation changes can be detected by attaching an event listener for orientationchange event in the Ti.Gesture module:
The updated device orientation can be read from orientation property of the event object passed to the callback, which will be defined as one of:
[list]
[*]Ti.UI.PORTRAIT
[*]Ti.UI.UPSIDE_PORTRAIT
[*]Ti.UI.LANDSCAPE_LEFT
[*]Ti.UI.LANDSCAPE_RIGHT
[/list]
Using our helper function above, you might use the following code to redraw your application UI based on device orientation:
[size=large][b]Changing Device Orientation Programmatically[/b][/size]
There are two ways of changing the device orientation in JavaScript. One can modify Ti.UI global value or limit the number of supported orientations for a given window object.
[size=large][b]Changing the global orientation[/b][/size]
First, you can change it directly by updating the value of Ti.UI.orientation property with the appropriate orientation constant:
This approach is recommended if you need to permanently change device orientation and stick to it. You can accomplish the same thing using the orientation section of the tiapp.xml file (for iOS).
[size=large][b]Limiting supported orientation modes for a given window[/b][/size]
You can limit allowed orientations for a Window object. Then device will go into the desired orientation whenever the window is opened:
This specific window will support only landscape mode, so whenever you call win.open() or Ti.UI.currentTab.open(win) the device will go landscape. Whenever you close close the window (or user navigate back using navigation controller) the screen will return back to the actual physical orientation of the device.
You can also update orientationModes of existing windows (including the opened one):
[size=large][b]Run-time Device Orientation Detection[/b][/size]
You can detect the current device orientation by checking value of the Ti.UI.orientation property. This value will match one of the orientation constants defined under the Ti.UI namespace:
[list]
[*]Ti.UI.PORTRAIT
[*]Ti.UI.UPSIDE_PORTRAIT
[*]Ti.UI.LANDSCAPE_LEFT
[*]Ti.UI.LANDSCAPE_RIGHT
[/list]
An example use of this property might be to define helper functions to determine if the device is in a landscape or portrait orientation:
Ti.Gesture.isLandscape = function (orient) {
orient = orient || Ti.UI.orientation;
return orient == Ti.UI.LANDSCAPE_LEFT || orient == Ti.UI.LANDSCAPE_RIGHT;
};
Ti.Gesture.isPortrait = function (orient) {
orient = orient || Ti.UI.orientation;
return orient == Ti.UI.PORTRAIT || orient == Ti.UI.UPSIDE_PORTRAIT;
};
[size=large][b]Handling Orientation Changes[/b][/size]
Orientation changes can be detected by attaching an event listener for orientationchange event in the Ti.Gesture module:
Ti.Gesture.addEventListener('orientationchange', function (e) {
// Put your handling code here
});
The updated device orientation can be read from orientation property of the event object passed to the callback, which will be defined as one of:
[list]
[*]Ti.UI.PORTRAIT
[*]Ti.UI.UPSIDE_PORTRAIT
[*]Ti.UI.LANDSCAPE_LEFT
[*]Ti.UI.LANDSCAPE_RIGHT
[/list]
Using our helper function above, you might use the following code to redraw your application UI based on device orientation:
Ti.Gesture.addEventListener('orientationchange', function (ev) {
if (Ti.Gesture.isLandscape(ev.orientation)) {
// Update your UI for landscape orientation
} else {
// Update your UI for portrait orientation
}
});
[size=large][b]Changing Device Orientation Programmatically[/b][/size]
There are two ways of changing the device orientation in JavaScript. One can modify Ti.UI global value or limit the number of supported orientations for a given window object.
[size=large][b]Changing the global orientation[/b][/size]
First, you can change it directly by updating the value of Ti.UI.orientation property with the appropriate orientation constant:
Ti.UI.orientation = Ti.UI.PORTRAIT;
This approach is recommended if you need to permanently change device orientation and stick to it. You can accomplish the same thing using the orientation section of the tiapp.xml file (for iOS).
[size=large][b]Limiting supported orientation modes for a given window[/b][/size]
You can limit allowed orientations for a Window object. Then device will go into the desired orientation whenever the window is opened:
var win = Ti.UI.createWindow({
width: '100%',
height: '100%',
orientationModes: [
Titanium.UI.LANDSCAPE_LEFT,
Titanium.UI.LANDSCAPE_RIGHT
]
});
This specific window will support only landscape mode, so whenever you call win.open() or Ti.UI.currentTab.open(win) the device will go landscape. Whenever you close close the window (or user navigate back using navigation controller) the screen will return back to the actual physical orientation of the device.
You can also update orientationModes of existing windows (including the opened one):
Ti.UI.currentWindow.orientationModes = [
Titanium.UI.LANDSCAPE_LEFT,
Titanium.UI.LANDSCAPE_RIGHT
];