`
rensanning
  • 浏览: 3574015 次
  • 性别: Icon_minigender_1
  • 来自: 大连
博客专栏
Efef1dba-f7dd-3931-8a61-8e1c76c3e39f
使用Titanium Mo...
浏览量:38696
Bbab2146-6e1d-3c50-acd6-c8bae29e307d
Cordova 3.x入门...
浏览量:609417
C08766e7-8a33-3f9b-9155-654af05c3484
常用Java开源Libra...
浏览量:685010
77063fb3-0ee7-3bfa-9c72-2a0234ebf83e
搭建 CentOS 6 服...
浏览量:90822
E40e5e76-1f3b-398e-b6a6-dc9cfbb38156
Spring Boot 入...
浏览量:403502
Abe39461-b089-344f-99fa-cdfbddea0e18
基于Spring Secu...
浏览量:70082
66a41a70-fdf0-3dc9-aa31-19b7e8b24672
MQTT入门
浏览量:92513
社区版块
存档分类
最新评论

在Titanium中处理手机的方向

阅读更多
【原文】Handling Device Orientation

Run-time Device Orientation Detection
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:

  • Ti.UI.PORTRAIT
  • Ti.UI.UPSIDE_PORTRAIT
  • Ti.UI.LANDSCAPE_LEFT
  • Ti.UI.LANDSCAPE_RIGHT

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;
};


Handling Orientation Changes
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:

  • Ti.UI.PORTRAIT
  • Ti.UI.UPSIDE_PORTRAIT
  • Ti.UI.LANDSCAPE_LEFT
  • Ti.UI.LANDSCAPE_RIGHT

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
  }
});


Changing Device Orientation Programmatically
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.

Changing the global orientation
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).

Limiting supported orientation modes for a given window
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
];


分享到:
评论

相关推荐

    android面试题库

    13. **LinearLayout方向**:在LinearLayout布局中,设置属性android:orientation为“vertical”意味着子元素将垂直排列。 14. **按键事件拦截**:为按钮设置OnKeyListener监听事件,如果在onKey()方法中返回true,则...

    工程硕士学位论文 基于Android+HTML5的移动Web项目高效开发探究

    目前市场业务中在产品以及其他项目的认证和检测方面存在诸多不便,用户需要实地考察并频繁与检测单位沟通,填写繁琐的纸质检测报告、当面送递样品,对于检测环节中存在的问题难以及时交互并处理。市场上相应的检测...

    Web App开发框架介绍及分析_刘铁锋

    此外,HTML5还增强了对硬件的访问能力,如WebGL允许在网页中创建复杂的3D图形。 ### Web开发框架现状与分析 面对HTML5带来的机遇,一系列Web开发框架应运而生,旨在简化Web App的开发过程。这些框架包括但不限于...

    移动应用跨平台情况调查及分析

    它允许开发者创建一个看起来像原生应用但实际上大部分内容是在Web视图中呈现的应用。 - **特点**: - 需要通过应用商店下载并安装。 - 可以利用本地资源(如摄像头、地理位置等)。 - 能够提供接近原生的用户...

    ComSocial:社会交流

    在ComSocial中,JavaScript可能用于处理用户交互,如点击按钮发送消息,滚动加载更多内容,或者实时更新用户的状态。 5. **框架与库的应用**:为了简化开发过程,ComSocial可能使用了前端框架,如Bootstrap或Vue.js...

Global site tag (gtag.js) - Google Analytics