跳过内容

开始

GitHub
终端窗口
bun add @capgo/capacitor-screen-orientation
bunx cap sync

导入

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

API概述

API概述

orientation

__CAPGO_KEEP_0__

获取当前屏幕方向

获取设备屏幕当前方向

import { ScreenOrientation } from '@capgo/capacitor-screen-orientation';
const result = await ScreenOrientation.orientation();
console.log('Current orientation:', result.type);

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

锁定屏幕到指定方向。 在 iOS 上,如果 bypassOrientationLock 为 true,会使用运动传感器 开始跟踪物理设备方向。

注意:UI 还是会尊严用户的方向锁定设置。 运动跟踪允许您检测设备如何物理上被握持 即使 UI 没有旋转。

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

解锁屏幕方向。

允许屏幕根据设备位置旋转自由。 也停止任何基于运动的方向跟踪,如果它启用了。

import { ScreenOrientation } from '@capgo/capacitor-screen-orientation';
await ScreenOrientation.unlock();

startOrientationTracking

标题为“开始方向跟踪”

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

此方法在您想要独立于屏幕方向锁定跟踪设备物理方向时很有用。 它使用 Core Motion 在 iOS 上检测方向变化。

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

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

停止使用运动传感器开始的方向跟踪

import { ScreenOrientation } from '@capgo/capacitor-screen-orientation';
await ScreenOrientation.stopOrientationTracking();

检查设备方向锁是否当前启用

该方法将物理设备方向(来自运动传感器)与UI方向进行比较。如果它们不同,方向锁已启用。

注意:此方法需要通过startOrientationTracking()或lock()(bypassOrientationLock:true)激活运动跟踪。适用于iOS(Core Motion)和Android(Accelerometer)。

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

由 orientation() 方法返回的结果。

export interface ScreenOrientationResult {
/**
* The current orientation type.
*
* @since 1.0.0
*/
type: OrientationType;
}

屏幕方向锁定选项

export interface OrientationLockOptions {
/**
* The orientation type to lock to.
*
* @since 1.0.0
*/
orientation: OrientationLockType;
/**
* Whether to track physical device orientation using motion sensors.
* When true, uses device motion sensors to detect the true physical
* orientation of the device, even when the device orientation lock is enabled.
*
* **Important:** This does NOT bypass the UI orientation lock.
* The screen will still respect the user's orientation lock setting.
* This option only affects orientation detection/tracking - you'll receive
* orientation change events based on how the device is physically held,
* but the UI will not rotate if orientation lock is enabled.
*
* Supported on iOS (Core Motion) and Android (Accelerometer).
*
* @default false
* @since 1.0.0
*/
bypassOrientationLock?: boolean;
}

使用运动传感器开始方向跟踪选项

export interface StartOrientationTrackingOptions {
/**
* Whether to track physical device orientation using motion sensors.
* When true, uses device motion sensors to detect the true physical
* orientation of the device, even when the device orientation lock is enabled.
*
* **Important:** This does NOT bypass the UI orientation lock.
* This only enables detection of the physical orientation.
*
* Supported on iOS (Core Motion) and Android (Accelerometer).
*
* @default false
* @since 1.0.0
*/
bypassOrientationLock?: boolean;
}

由 isOrientationLocked() 方法返回的结果。

export interface OrientationLockStatusResult {
/**
* Whether the device orientation lock is currently enabled.
*
* This is determined by comparing the physical device orientation
* (from motion sensors) with the UI orientation. If they differ,
* orientation lock is enabled.
*
* Available on iOS (Core Motion) and Android (Accelerometer) when motion tracking is active.
*
* @since 1.0.0
*/
locked: boolean;
/**
* The physical orientation of the device from motion sensors.
* Available when motion tracking is active (iOS and Android).
*
* @since 1.0.0
*/
physicalOrientation?: OrientationType;
/**
* The current UI orientation reported by the system.
*
* @since 1.0.0
*/
uiOrientation: OrientationType;
}

设备方向类型,描述设备方向状态。

export type OrientationType = 'portrait-primary' | 'portrait-secondary' | 'landscape-primary' | 'landscape-secondary';

OrientationLockType

标题:方向锁定类型

用于锁定设备方向的方向锁定类型。

export type OrientationLockType =
| 'any'
| 'natural'
| 'landscape'
| 'portrait'
| 'portrait-primary'
| 'portrait-secondary'
| 'landscape-primary'
| 'landscape-secondary';

真实数据来源

标题:真实数据来源

本页面是根据插件生成的。 src/definitions.ts当公共API上游发生变化时,请重新运行同步。

如果您正在使用 开始使用 为原生媒体和界面行为做好准备,连接它与 使用 @capgo/capacitor-屏幕方向 为原生功能在使用 @capgo/capacitor-屏幕方向中 使用 @capgo/capacitor-实时活动 为原生功能在使用 @capgo/capacitor-实时活动中 @capgo/capacitor-实时活动 为 @capgo/capacitor-实时活动的实现细节 使用 @capgo/capacitor-视频播放器 为原生功能在使用 @capgo/capacitor-视频播放器中 @capgo/capacitor-视频播放器 为 @capgo/capacitor-视频播放器的实现细节