Langsung ke konten

Memulai

  1. Instal paket

    Terminal window
    npm i @capgo/capacitor-screen-orientation
  2. Sinkronisasi dengan proyek asli

    Terminal window
    npx cap sync
  3. iOS Konfigurasi (opsional) Untuk mendeteksi orientasi perangkat fisik menggunakan sensor gerak di iOS, tambahkan ke Info.plist Anda:

    <key>NSMotionUsageDescription</key>
    <string>This app uses motion sensors to detect device orientation.</string>
import { ScreenOrientation } from '@capgo/capacitor-screen-orientation';
// Get current orientation
const current = await ScreenOrientation.orientation();
console.log('Current orientation:', current.type);
// Lock to landscape
await ScreenOrientation.lock({ orientation: 'landscape' });
// Unlock orientation
await ScreenOrientation.unlock();
// Listen for orientation changes
const listener = await ScreenOrientation.addListener(
'screenOrientationChange',
(result) => {
console.log('Orientation changed:', result.type);
}
);

Plugin ini memiliki fitur unik: plugin ini dapat mendeteksi orientasi fisik sebenarnya perangkat menggunakan sensor gerak, bahkan ketika pengguna telah mengaktifkan kunci orientasi pada perangkatnya.

import { ScreenOrientation } from '@capgo/capacitor-screen-orientation';
// Start motion-based tracking
await ScreenOrientation.startOrientationTracking({
bypassOrientationLock: true
});
// Now orientation change events will reflect physical orientation
const listener = await ScreenOrientation.addListener(
'screenOrientationChange',
(result) => {
console.log('Physical orientation:', result.type);
}
);
// Check if orientation lock is enabled
const 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 done
await ScreenOrientation.stopOrientationTracking();

Dapatkan orientasi layar saat ini.

const result = await ScreenOrientation.orientation();
// Returns: { type: OrientationType }

Nilai Tipe Orientasi:

  • 'potret-utama' - Potret, tombol beranda di bagian bawah
  • 'potret-sekunder' - Potret, terbalik
  • 'landscape-primary' - Landscape, tombol beranda di sebelah kanan
  • 'lanskap-sekunder' - Lanskap, tombol beranda di sebelah kiri

Kunci layar ke orientasi tertentu.

interface OrientationLockOptions {
orientation: OrientationLockType;
bypassOrientationLock?: boolean; // Enable motion tracking
}
await ScreenOrientation.lock({ orientation: 'landscape' });

Nilai OrientationLockType:

  • 'apa saja' - Orientasi apa pun
  • 'alami' - Orientasi alami perangkat
  • 'lanskap' - Mode lanskap apa pun
  • 'potret' - Mode potret apa pun
  • 'potret-primer' / 'potret-sekunder'
  • 'lanskap-primer' / 'lanskap-sekunder'

Buka kunci orientasi layar.

await ScreenOrientation.unlock();

Mulai lacak orientasi perangkat fisik menggunakan sensor gerak.

await ScreenOrientation.startOrientationTracking({
bypassOrientationLock: true
});

Hentikan pelacakan orientasi berbasis gerakan.

await ScreenOrientation.stopOrientationTracking();

Periksa apakah kunci orientasi perangkat diaktifkan.

const result = await ScreenOrientation.isOrientationLocked();
// Returns: {
// locked: boolean,
// physicalOrientation?: OrientationType,
// uiOrientation?: OrientationType
// }

Dengarkan perubahan orientasi.

const listener = await ScreenOrientation.addListener(
'screenOrientationChange',
(result) => {
console.log('New orientation:', result.type);
}
);
// Remove when done
await listener.remove();

Hapus semua pendengar acara.

await ScreenOrientation.removeAllListeners();
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();
}
}
}
  • Menggunakan kerangka Core Motion untuk deteksi orientasi fisik
  • Membutuhkan NSMotionUsageDescription di Info.plist untuk sensor gerak
  • Dukungan penuh untuk semua jenis orientasi
  • Menggunakan sensor accelerometer untuk deteksi orientasi fisik
  • Tidak diperlukan izin tambahan
  • Dukungan penuh untuk semua jenis orientasi
  • Menggunakan Orientasi Layar API
  • Deteksi sensor gerak terbatas
  • Beberapa browser mungkin tidak mendukung semua jenis kunci