在Titanium中处理手机的方向

【原文】[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:


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
];
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值