Iniziare
-
Installa il pacchetto
Terminal window npm i @capgo/capacitor-screen-orientationTerminal window pnpm add @capgo/capacitor-screen-orientationTerminal window yarn add @capgo/capacitor-screen-orientationTerminal window bun add @capgo/capacitor-screen-orientation -
Sincronizzazione con progetti nativi
Terminal window npx cap syncTerminal window pnpm cap syncTerminal window yarn cap syncTerminal window bunx cap sync -
iOS Configurazione (opzionale) Per rilevare l’orientamento del dispositivo fisico utilizzando i sensori di movimento su iOS, aggiungi al tuo
Info.plist:<key>NSMotionUsageDescription</key><string>This app uses motion sensors to detect device orientation.</string>
Utilizzo di base
Section titled “Utilizzo di base”import { ScreenOrientation } from '@capgo/capacitor-screen-orientation';
// Get current orientationconst current = await ScreenOrientation.orientation();console.log('Current orientation:', current.type);
// Lock to landscapeawait ScreenOrientation.lock({ orientation: 'landscape' });
// Unlock orientationawait ScreenOrientation.unlock();
// Listen for orientation changesconst listener = await ScreenOrientation.addListener( 'screenOrientationChange', (result) => { console.log('Orientation changed:', result.type); });Rilevamento dell’orientamento fisico
Section titled “Rilevamento dell’orientamento fisico”Questo plugin ha una caratteristica unica: può rilevare il vero orientamento fisico del dispositivo utilizzando sensori di movimento, anche quando l’utente ha abilitato il blocco dell’orientamento sul proprio dispositivo.
import { ScreenOrientation } from '@capgo/capacitor-screen-orientation';
// Start motion-based trackingawait ScreenOrientation.startOrientationTracking({ bypassOrientationLock: true});
// Now orientation change events will reflect physical orientationconst listener = await ScreenOrientation.addListener( 'screenOrientationChange', (result) => { console.log('Physical orientation:', result.type); });
// Check if orientation lock is enabledconst lockStatus = await ScreenOrientation.isOrientationLocked();if (lockStatus.locked) { console.log('User has orientation lock enabled'); console.log('Physical:', lockStatus.physicalOrientation); console.log('UI:', lockStatus.uiOrientation);}
// Stop tracking when doneawait ScreenOrientation.stopOrientationTracking();API Riferimento
Section titled “API Riferimento”orientamento()
Section titled “orientamento()”Ottieni l’orientamento corrente dello schermo.
const result = await ScreenOrientation.orientation();// Returns: { type: OrientationType }Valori OrientationType:
'ritratto-primario'- Ritratto, pulsante home in basso'ritratto-secondario'- Ritratto, capovolto'landscape-primary'- Paesaggio, pulsante home a destra'landscape-secondary'- Paesaggio, pulsante home a sinistra
serratura(opzioni)
Section titled “serratura(opzioni)”Blocca lo schermo su un orientamento specifico.
interface OrientationLockOptions { orientation: OrientationLockType; bypassOrientationLock?: boolean; // Enable motion tracking}
await ScreenOrientation.lock({ orientation: 'landscape' });Valori OrientationLockType:
'any'- Qualsiasi orientamento'natural'- Orientamento naturale del dispositivo'landscape'- Qualsiasi modalità orizzontale'ritratto'- Qualsiasi modalità ritratto- “ritratto-primario” / “ritratto-secondario”.
'paesaggio-primario'/'paesaggio-secondario'
sblocca()
Section titled “sblocca()”Sblocca l’orientamento dello schermo.
await ScreenOrientation.unlock();startOrientationTracking(opzioni?)
Section titled “startOrientationTracking(opzioni?)”Inizia a monitorare l’orientamento del dispositivo fisico utilizzando i sensori di movimento.
await ScreenOrientation.startOrientationTracking({ bypassOrientationLock: true});stopOrientamentoTracciamento()
Section titled “stopOrientamentoTracciamento()”Arresta il tracciamento dell’orientamento basato sul movimento.
await ScreenOrientation.stopOrientationTracking();isOrientationLocked()
Section titled “isOrientationLocked()”Controlla se il blocco dell’orientamento del dispositivo è abilitato.
const result = await ScreenOrientation.isOrientationLocked();// Returns: {// locked: boolean,// physicalOrientation?: OrientationType,// uiOrientation?: OrientationType// }addListener(nomeevento, callback)
Section titled “addListener(nomeevento, callback)”Ascolta i cambiamenti di orientamento.
const listener = await ScreenOrientation.addListener( 'screenOrientationChange', (result) => { console.log('New orientation:', result.type); });
// Remove when doneawait listener.remove();rimuoviTuttiGliAscoltatori()
Section titled “rimuoviTuttiGliAscoltatori()”Rimuovi tutti i listener di eventi.
await ScreenOrientation.removeAllListeners();Esempio completo
Section titled “Esempio completo”import { ScreenOrientation } from '@capgo/capacitor-screen-orientation';
class OrientationManager { private listener: any = null;
async init() { // Start motion tracking for physical orientation detection await ScreenOrientation.startOrientationTracking({ bypassOrientationLock: true });
// Listen for changes this.listener = await ScreenOrientation.addListener( 'screenOrientationChange', this.onOrientationChange.bind(this) );
// Get initial orientation const { type } = await ScreenOrientation.orientation(); console.log('Initial orientation:', type); }
onOrientationChange(result: { type: string }) { console.log('Orientation changed to:', result.type);
// Adjust UI based on orientation if (result.type.includes('landscape')) { this.showLandscapeUI(); } else { this.showPortraitUI(); } }
showLandscapeUI() { // Landscape-specific UI adjustments }
showPortraitUI() { // Portrait-specific UI adjustments }
async lockLandscape() { await ScreenOrientation.lock({ orientation: 'landscape' }); }
async lockPortrait() { await ScreenOrientation.lock({ orientation: 'portrait' }); }
async freeRotation() { await ScreenOrientation.unlock(); }
async checkIfUserHasOrientationLock() { const { locked, physicalOrientation, uiOrientation } = await ScreenOrientation.isOrientationLocked();
if (locked) { console.log(`Device is held in ${physicalOrientation} but UI shows ${uiOrientation}`); return true; } return false; }
async destroy() { await ScreenOrientation.stopOrientationTracking(); if (this.listener) { await this.listener.remove(); } }}Note sulla piattaforma
Section titled “Note sulla piattaforma”- Utilizza il framework Core Motion per il rilevamento dell’orientamento fisico
- Richiede
NSMotionUsageDescriptionin Info.plist per i sensori di movimento - Pieno supporto per tutti i tipi di orientamento
Android
Section titled “Android”- Utilizza il sensore dell’accelerometro per il rilevamento dell’orientamento fisico
- Non sono necessarie autorizzazioni aggiuntive
- Pieno supporto per tutti i tipi di orientamento
- Utilizza l’orientamento dello schermo API
- Il rilevamento del sensore di movimento è limitato
- Alcuni browser potrebbero non supportare tutti i tipi di blocco