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

在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
];


分享到:
评论

相关推荐

    Titanium中支持IOS设备的拖拽

    3. **定义拖动代理**:这是iOS原生的一部分,但在Titanium中,你可能需要创建一个JavaScript对象来模拟这个代理,处理拖动过程中的事件,如开始拖动、拖动中和结束拖动。 4. **创建放置目标**:这涉及设置其他可以...

    Google-Cloud-Messaging--Titanium-, 在 Titanium 中,Google云消息传递.zip

    Google-Cloud-Messaging--Titanium-, 在 Titanium 中,Google云消息传递 Google-Cloud-Messaging--Titanium -注册带有GCM和处理发送到设备的通知的Titanium MODULE 。Android平台使用c2dm进行推送,但是因为c2dm停止...

    TITANIUM智能手机应用开发教程

    ### TITANIUM智能手机应用开发教程知识点概览 #### 一、TITANIUM系统简介与特点 **TITANIUM系统**是一种基于云的移动应用程序开发平台,它支持跨平台开发,允许开发者使用JavaScript来编写原生移动应用程序。...

    TitaniumBackup_6.0.5.1

    在安卓设备的管理和维护中,数据备份与恢复是至关重要的环节。TitaniumBackup_6.0.5.1,这款专业版应用,以其强大的功能和高效的操作,为用户提供了可靠的数据管理工具。它的静默恢复特性,更是为用户带来了前所未有...

    titanium 打开本地网络

    在Titanium中,可以使用`Ti.Network`模块来检查设备的网络状态。这个模块提供了多种方法来获取网络连接的信息,例如`Titanium.Network.online`属性,它返回一个布尔值,表示设备当前是否已连接到任何类型的网络(如...

    Titanium中文版开发手册

    控制器是Alloy框架中的关键组件,它们负责处理用户交互和业务逻辑。这一章节会介绍如何定义和使用控制器,以及如何在控制器之间传递数据。 5. **Alloy Styles and Themes(样式和主题)** 样式和主题使得开发者...

    在Titanium中通过使用BEGIN/COMMIT来加速SQLite插入操作

    通过合理使用BEGIN/COMMIT事务,开发者可以在Titanium中实现更高效的数据插入,提高应用性能,尤其在处理大量数据时效果显著。同时,结合其他优化策略,如SQL语句优化,可以进一步提升数据库操作的效率。

    Titanium中Httpclient访问REST 服务

    接下来,我们来看在Titanium中如何使用HttpClient访问REST服务。以下是一个基本的示例: ```javascript var http = Ti.Network.HTTPClient; var url = 'http://example.com/api/resource'; http.onload = function...

    Titanium Mobile API

    在 Titanium Mobile API 中,核心组成部分是 Titanium Module。这些模块包含了大量预定义的对象和方法,旨在简化常见的移动开发任务。例如: - **Ti.UI**:用户界面组件,包括按钮、文本框、列表视图等。 - **Ti....

    Titanium plugin开发初探

    完成后,可以打包成 `.tibundle` 或 `.aar` 文件,供其他开发者在他们的 Titanium 项目中使用。 **8. 源码分析** `billdawson-ti_coffee_plugin-7dc9a53` 这个文件名可能指的是一个具体的 Titanium 插件项目,由 ...

    Titanium Backup Pro 8.0.0.apk

    功能很强大,有此软件,不管你的格机还是重刷ROM,在重装软件时都是无痛的,非实适用经常捣鼓手机有机友! 解锁器使用说明: 1.安装钛备份主程序到手机内存中,且不运行主程序。 2.安装钛备份解锁器工具。 3.打开钛...

    Titanium中SOAPClient访问SOAP协议的WebService

    在Titanium中,可以使用内置的XML解析器或者第三方库如xml2js来处理XML数据。 通过阅读`TitaniumSOAPWebService`这个压缩包中的文件,你可以找到更具体的示例和实践,进一步了解如何在Titanium项目中实现SOAP客户端...

    Titanium Backup_3.7.4捐赠完全版

    Titanium Backup_3.7.4捐赠完全版

    Titanium资料

    UIView 是 iOS 平台上视图的基础类,它在 Titanium 中被用来创建和管理用户界面元素。通过这个文档,你可以了解到如何创建、布局和交互各种 UI 元素,如按钮、文本框、图片视图等,以及如何添加事件监听器来响应用户...

    [Titanium] Appcelerator Titanium 移动应用开发教程 (英文版)

    [Packt Publishing] Appcelerator Titanium 移动应用开发教程 (英文版) [Packt Publishing] Creating Mobile Apps with Appcelerator Titanium (E-Book) ☆ 图书概要:☆ Develop fully-featured mobile ...

    titanium-d1-kickstart.6.1.1

    titanium-d1-kickstart.6.1.1.gbin 强大的思科模拟器

    Titanium_ziliao

    在"Titanium_ziliao"这个项目中,我们主要探讨的是钛合金(Titanium Alloy)的相关知识。钛合金是一种结合了钛元素和其他金属元素的合金材料,它在多个领域,如航空航天、医疗器械、体育用品等,都有广泛的应用。...

    appc-example-app-images:示例应用程序展示了如何在 Titanium 中处理图像

    在处理网络图像时,通常需要考虑性能和用户体验。Titanium 提供了 `Ti.Network.HTTPClient` 对象来异步下载和加载图像。通过监听 `load` 事件,可以将远程图像数据转换为本地 `Blob` 对象,然后将其设置到 `...

Global site tag (gtag.js) - Google Analytics