Erste Schritte mit Screen Orientation
-
Paket installieren
Terminal-Fenster npm i @capgo/capacitor-screen-orientationTerminal-Fenster pnpm add @capgo/capacitor-screen-orientationTerminal-Fenster yarn add @capgo/capacitor-screen-orientationTerminal-Fenster bun add @capgo/capacitor-screen-orientation -
Mit nativen Projekten synchronisieren
Terminal-Fenster npx cap syncTerminal-Fenster pnpm cap syncTerminal-Fenster yarn cap syncTerminal-Fenster bunx cap sync -
iOS-Konfiguration (optional) Um physikalische Geräteorientierung mit Bewegungssensoren auf iOS zu erkennen, fügen Sie zu Ihrer
Info.plisthinzu:<key>NSMotionUsageDescription</key><string>Diese App verwendet Bewegungssensoren zur Erkennung der Geräteorientierung.</string>
Grundlegende Verwendung
Section titled “Grundlegende Verwendung”import { ScreenOrientation } from '@capgo/capacitor-screen-orientation';
// Aktuelle Orientierung abrufenconst current = await ScreenOrientation.orientation();console.log('Aktuelle Orientierung:', current.type);
// Im Landscape-Modus sperrenawait ScreenOrientation.lock({ orientation: 'landscape' });
// Orientierung entsperrenawait ScreenOrientation.unlock();
// Auf Orientierungsänderungen abhörenconst listener = await ScreenOrientation.addListener( 'screenOrientationChange', (result) => { console.log('Orientierung geändert:', result.type); });Erkennung der physikalischen Orientierung
Section titled “Erkennung der physikalischen Orientierung”Dieses Plugin hat eine einzigartige Funktion: Es kann die wahre physikalische Orientierung des Geräts unter Verwendung von Bewegungssensoren erkennen, auch wenn der Benutzer die Orientierungssperre auf seinem Gerät aktiviert hat.
import { ScreenOrientation } from '@capgo/capacitor-screen-orientation';
// Motion-basiertes Tracking startenawait ScreenOrientation.startOrientationTracking({ bypassOrientationLock: true});
// Jetzt zeigen Orientierungsänderungsereignisse die physikalische Orientierungconst listener = await ScreenOrientation.addListener( 'screenOrientationChange', (result) => { console.log('Physikalische Orientierung:', result.type); });
// Überprüfen Sie, ob Orientierungssperre aktiviert istconst lockStatus = await ScreenOrientation.isOrientationLocked();if (lockStatus.locked) { console.log('Benutzer hat Orientierungssperre aktiviert'); console.log('Physisch:', lockStatus.physicalOrientation); console.log('UI:', lockStatus.uiOrientation);}
// Tracking beenden, wenn fertigawait ScreenOrientation.stopOrientationTracking();API-Referenz
Section titled “API-Referenz”orientation()
Section titled “orientation()”Rufen Sie die aktuelle Bildschirmausrichtung ab.
const result = await ScreenOrientation.orientation();// Rückgabe: { type: OrientationType }OrientationType-Werte:
'portrait-primary'- Hochformat, Startknopf unten'portrait-secondary'- Hochformat, umgekehrt'landscape-primary'- Querformat, Startknopf rechts'landscape-secondary'- Querformat, Startknopf links
lock(options)
Section titled “lock(options)”Sperren Sie den Bildschirm auf eine bestimmte Ausrichtung.
interface OrientationLockOptions { orientation: OrientationLockType; bypassOrientationLock?: boolean; // Motion Tracking aktivieren}
await ScreenOrientation.lock({ orientation: 'landscape' });OrientationLockType-Werte:
'any'- Beliebige Orientierung'natural'- Natürliche Ausrichtung des Geräts'landscape'- Beliebiger Landscape-Modus'portrait'- Beliebiger Portrait-Modus'portrait-primary'/'portrait-secondary''landscape-primary'/'landscape-secondary'
unlock()
Section titled “unlock()”Entsperren Sie die Bildschirmausrichtung.
await ScreenOrientation.unlock();startOrientationTracking(options?)
Section titled “startOrientationTracking(options?)”Beginnen Sie mit der Verfolgung der physikalischen Geräteausrichtung mit Bewegungssensoren.
await ScreenOrientation.startOrientationTracking({ bypassOrientationLock: true});stopOrientationTracking()
Section titled “stopOrientationTracking()”Stoppen Sie die Motion-basierte Orientierungsverfolgung.
await ScreenOrientation.stopOrientationTracking();isOrientationLocked()
Section titled “isOrientationLocked()”Überprüfen Sie, ob die Geräteorientierungssperre aktiviert ist.
const result = await ScreenOrientation.isOrientationLocked();// Rückgabe: {// locked: boolean,// physicalOrientation?: OrientationType,// uiOrientation?: OrientationType// }addListener(eventName, callback)
Section titled “addListener(eventName, callback)”Abhören auf Orientierungsänderungen.
const listener = await ScreenOrientation.addListener( 'screenOrientationChange', (result) => { console.log('Neue Orientierung:', result.type); });
// Entfernen Sie, wenn fertigawait listener.remove();removeAllListeners()
Section titled “removeAllListeners()”Entfernen Sie alle Event-Listener.
await ScreenOrientation.removeAllListeners();Vollständiges Beispiel
Section titled “Vollständiges Beispiel”import { ScreenOrientation } from '@capgo/capacitor-screen-orientation';
class OrientationManager { private listener: any = null;
async init() { // Motion Tracking für Erkennung der physikalischen Ausrichtung starten await ScreenOrientation.startOrientationTracking({ bypassOrientationLock: true });
// Auf Änderungen abhören this.listener = await ScreenOrientation.addListener( 'screenOrientationChange', this.onOrientationChange.bind(this) );
// Anfängliche Orientierung abrufen const { type } = await ScreenOrientation.orientation(); console.log('Anfängliche Orientierung:', type); }
onOrientationChange(result: { type: string }) { console.log('Orientierung geändert zu:', result.type);
// Benutzeroberfläche basierend auf Orientierung anpassen if (result.type.includes('landscape')) { this.showLandscapeUI(); } else { this.showPortraitUI(); } }
showLandscapeUI() { // Landscape-spezifische UI-Anpassungen }
showPortraitUI() { // Portrait-spezifische UI-Anpassungen }
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(`Gerät wird in ${physicalOrientation} gehalten, aber UI zeigt ${uiOrientation}`); return true; } return false; }
async destroy() { await ScreenOrientation.stopOrientationTracking(); if (this.listener) { await this.listener.remove(); } }}Plattformnotizen
Section titled “Plattformnotizen”- Verwendet Core Motion Framework für Erkennung der physikalischen Ausrichtung
- Erfordert
NSMotionUsageDescriptionin Info.plist für Bewegungssensoren - Vollständige Unterstützung für alle Ausrichtungstypen
Android
Section titled “Android”- Verwendet Beschleunigungssensor für Erkennung der physikalischen Ausrichtung
- Keine zusätzlichen Berechtigungen erforderlich
- Vollständige Unterstützung für alle Ausrichtungstypen
- Verwendet Screen Orientation API
- Bewegungssensor-Erkennung ist begrenzt
- Einige Browser unterstützen möglicherweise nicht alle Lock-Typen