使用@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-视频播放器 的原生能力。