@capacitor/screen-orientation
屏幕方向API提供与屏幕方向相关的信息和功能。
安装
npm install @capacitor/screen-orientation
npx cap sync
iOS平台说明
屏幕方向锁定功能仅对Capacitor视图控制器有效,不会影响其他模态视图控制器(如浏览器插件打开的视图)。若需同时锁定模态视图的方向,可在应用的AppDelegate.swift
文件中添加以下代码:
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
return UIInterfaceOrientationMask(rawValue: (self.window!.rootViewController as! CAPBridgeViewController).supportedInterfaceOrientations.rawValue)
}
iPad方向锁定
默认情况下,iPad支持多任务处理且无法锁定方向。如需在iPad上锁定方向,需将Info.plist
中的Requires Full Screen
选项设为YES
:
<key>UIRequiresFullScreen</key>
<true/>
API接口
orientation()
orientation() => Promise<ScreenOrientationResult>
获取当前屏幕方向。
返回值:
Promise<ScreenOrientationResult>
自版本: 4.0.0
lock(...)
lock(options: OrientationLockOptions) => Promise<void>
锁定屏幕方向。
参数 | 类型 |
---|---|
options |
|
自版本: 4.0.0
unlock()
unlock() => Promise<void>
解除屏幕方向锁定。
自版本: 4uallyx0
addListener('screenOrientationChange', ...)
addListener(eventName: 'screenOrientationChange', listenerFunc: (orientation: ScreenOrientationResult) => void) => Promise<PluginListenerHandle>
监听屏幕方向变化事件。
参数 | 类型 |
---|---|
eventName | 'screenOrientationChange' |
listenerFunc |
|
返回值:
Promise<PluginListenerHandle>
自版本: 4.0.0
removeAllListeners()
removeAllListeners() => Promise<void>
移除所有事件监听器。
自版本: 4.0.0
Interfaces
ScreenOrientationResult
属性 | 类型 |
---|---|
type | OrientationType |
OrientationLockOptions
属性 | 类型 | 说明 |
---|---|---|
orientation |
| 注意:TypeScript v5.2+用户应从@capacitor/screen-orientation导入OrientationLockType类型定义。 |
PluginListenerHandle
属性 | 类型 |
---|---|
remove | () => Promise<void> |
Type Aliases
OrientationLockType
'any' | 'natural' | 'landscape' | 'portrait' | 'portrait-primary' | 'portrait-secondary' | 'landscape-primary' |
'landscape-secondary'