跳过主要内容

屏幕方向 Capacitor插件

支持屏幕方向锁定

安装

bun add @capgo/capacitor-screen-orientation bunx cap sync
npm、pnpm或yarn
  • npm install @capgo/capacitor-screen-orientation
  • pnpm add @capgo/capacitor-screen-orientation
  • yarn add @capgo/capacitor-screen-orientation

指南

如何在Capacitor中使用屏幕方向

在设备上测试

下载Capgo应用,扫描二维码code

屏幕方向插件预览二维码code

使用@capgo/capacitor-screen-orientation

Capacitor屏幕方向插件接口

安装

bun add @capgo/capacitor-screen-orientation
bunx cap sync

bunx cap sync 将插件的本地code复制到您的本地项目中。每次升级插件后,请重新运行它。

导入

import { ScreenOrientation } from '@capgo/capacitor-screen-orientation';

API概览

方法 描述
orientation 获取当前屏幕方向
lock 锁定屏幕方向到特定类型
unlock 解锁屏幕方向
startOrientationTracking 使用运动传感器开始跟踪设备方向
stopOrientationTracking 使用运动传感器停止跟踪设备方向
isOrientationLocked 检查设备方向锁是否已启用
isDeviceFoldable 监听屏幕方向变化
getFoldState 读取当前折叠。
getHingeAngle 读取角度(度数)。
getReservedRegions 读取折叠和覆盖屏幕的任何内容。
getBarPlacement 读取iPhone Duo将原生标签栏和工具栏放置的位置。
setVerticalBarBehavior 选择是否允许iPhone Duo将此应用程序的栏移动到侧边。
getSizeClass 读取窗口大小类别。

示例

orientation()

读取当前屏幕方向。

import { ScreenOrientation } from '@capgo/capacitor-screen-orientation';

const result = await ScreenOrientation.orientation();
console.log('Current orientation:', result.type);

lock()

锁定屏幕方向到特定类型。

import { ScreenOrientation } from '@capgo/capacitor-screen-orientation';

// Standard lock
await ScreenOrientation.lock({ orientation: 'landscape' });

// Lock with motion tracking on iOS
await ScreenOrientation.lock({
  orientation: 'portrait',
  bypassOrientationLock: true
});

unlock()

解锁屏幕方向。

import { ScreenOrientation } from '@capgo/capacitor-screen-orientation';

await ScreenOrientation.unlock();

startOrientationTracking()

开始使用运动传感器跟踪设备方向。

import { ScreenOrientation } from '@capgo/capacitor-screen-orientation';

await ScreenOrientation.startOrientationTracking({
  bypassOrientationLock: true
});

// Listen for changes
ScreenOrientation.addListener('screenOrientationChange', (result) => {
  console.log('Orientation changed:', result.type);
});

stopOrientationTracking()

停止使用运动传感器跟踪设备方向。

import { ScreenOrientation } from '@capgo/capacitor-screen-orientation';

await ScreenOrientation.stopOrientationTracking();

isOrientationLocked()

检测设备方向锁是否已启用。

import { ScreenOrientation } from '@capgo/capacitor-screen-orientation';

// Start motion tracking first
await ScreenOrientation.startOrientationTracking({
  bypassOrientationLock: true
});

// Check lock status
const status = await ScreenOrientation.isOrientationLocked();
if (status.locked) {
  console.log('Orientation lock is ON');
  console.log('Physical:', status.physicalOrientation);
  console.log('UI:', status.uiOrientation);
}

上表列出了 13 个核心方法。监听器和版本助手,以及每个方法的完整契约,均在 GitHub 仓库.

监听事件

addListener 返回一个句柄。调用 handle.remove() 当屏幕卸载时,或 ScreenOrientation.removeAllListeners() 清除所有监听器。

全局参考

继续使用 @capgo/capacitor-screen-orientation

如果您正在使用 使用 @capgo/capacitor-screen-orientation 为了规划原生媒体和界面行为,连接它与 Capacitorcapgocapacitor屏幕方向 为了在 @capgo/capacitor-screen-orientation 中实现详细信息 开始 为了在开始中实现详细信息 使用 @capgo/capacitor-live-activities 为了在使用 @capgo/capacitor-live-activities 中实现原生能力 @capgo/capacitor-live-activities 为了在 @capgo/capacitor-live-activities 中实现详细信息,并且 使用 @capgo/capacitor-video-player 为使用 @capgo/capacitor-视频播放器 的原生能力。

常见问题

屏幕方向插件FAQ

如何在Capacitor应用中安装屏幕方向插件?

运行"bun add @capgo/capacitor-screen-orientation" (或"npm install @capgo/capacitor-screen-orientation"),然后运行"bunx cap sync"以便iOS和Android项目捕获native code。从"@capgo/capacitor-screen-orientation"导入你的应用code。

是否@capgo/capacitor-屏幕方向支持React、Vue和Angular?

是。屏幕方向是一个Capacitor包,因此它与任何运行在Capacitor中的web框架兼容,包括Ionic、React、Vue、Angular、Svelte和普通JavaScript。

哪个Capacitor版本支持@capgo/capacitor-screen-orientation?

Capgo插件遵循Capacitor的主要版本:使用与Capacitor主要版本匹配的插件主要版本(例如插件v8与Capacitor 8)。在GitHub README中列出的兼容性表格列出了维护的版本。

是否支持capgo/capacitor-screen-orientation?

是。code源代码在GitHub上公开,https://github.com/Cap-go/capacitor-screen-orientation/,并且该包可以从npm免费安装。欢迎bug报告和pull请求。

是否可以在不经过App Store审查的情况下更新使用屏幕方向的code?

安装或升级插件会更改native code,因此需要一个新的商店构建。之后,JavaScript、HTML和CSS更改可以立即通过Capgo实时更新发布。

不等待应用程序审查即可将屏幕方向变化推送到应用程序

一旦插件在您的商店构建中,Capgo即刻将您的JavaScript、HTML和CSS更改推送给用户。

开始使用Capgo