跳过主要内容
返回插件
@capgo/capacitor-屏幕方向
教程
由 github.com/Cap-go

屏幕方向

支持屏幕方向锁定跳过的屏幕方向插件

指南

屏幕方向的教程

使用@capgo/capacitor-screen-orientation

Capacitor屏幕方向插件接口

安装

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

此插件暴露的内容

  • orientation - 获取当前屏幕方向。
  • lock - 将屏幕方向锁定到特定类型。
  • unlock - 解锁屏幕方向。
  • startOrientationTracking - 使用运动传感器开始跟踪设备方向。

示例用途

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

全参考