はじめる
-
パッケージをインストールします
Terminal window npm i @capgo/capacitor-shakeTerminal window pnpm add @capgo/capacitor-shakeTerminal window yarn add @capgo/capacitor-shakeTerminal window bun add @capgo/capacitor-shake -
ネイティブ プロジェクトと同期
Terminal window npx cap syncTerminal window pnpm cap syncTerminal window yarn cap syncTerminal window bunx cap sync -
プラグインを設定します
基本的な使用例:
import { Shake } from '@capgo/capacitor-shake';// Start listening for shake gesturesawait Shake.start();// Listen for shake eventsShake.addListener('shake', () => {console.log('Device was shaken!');// Perform your action here});感度設定:
// Configure shake sensitivityawait Shake.start({threshold: 3.5 // Adjust sensitivity (default: 3.5)});iOS には追加の設定は必要ありません。
Android には追加の設定は必要ありません。
-
揺れを聞くのをやめる
// Stop shake detection when not neededawait Shake.stop();// Remove specific listenerconst handle = await Shake.addListener('shake', () => {console.log('Shaken!');});handle.remove(); -
実装例
import { Shake } from '@capgo/capacitor-shake';import { App } from '@capacitor/app';export class ShakeService {private shakeListener: any;async initialize() {// Start shake detectionawait Shake.start({ threshold: 4.0 });// Add shake listenerthis.shakeListener = await Shake.addListener('shake', () => {this.handleShake();});// Clean up on app pauseApp.addListener('pause', () => {Shake.stop();});// Resume on app resumeApp.addListener('resume', () => {Shake.start({ threshold: 4.0 });});}private handleShake() {console.log('Shake detected!');// Show debug menu, refresh data, or trigger any action}async cleanup() {if (this.shakeListener) {this.shakeListener.remove();}await Shake.stop();}}
API リファレンス
Section titled “API リファレンス”start(options?: ShakeOptions)
Section titled “start(options?: ShakeOptions)”シェイクジェスチャーを聞き始めます。
パラメータ:
options(オプション): 設定オブジェクトthreshold: 数値 - 感度のしきい値 (デフォルト: 3.5)
stop()
Section titled “stop()”シェイクジェスチャーを聞くのをやめてください。
addListener('shake', callback: () => void)
Section titled “addListener('shake', callback: () => void)”シェイクイベントのリスナーを追加します。
戻り値: リスナーを削除するハンドルを指定した Promise
インターフェース
Section titled “インターフェース”interface ShakeOptions { threshold?: number; // Shake sensitivity threshold}プラットフォームに関する注意事項
Section titled “プラットフォームに関する注意事項”- デバイスの加速度計を使用してシェイク ジェスチャを検出します
- フォアグラウンドとバックグラウンドの両方で動作します (アプリにバックグラウンド権限がある場合)
Android
Section titled “Android”- SensorManager を使用してシェイク イベントを検出します
- 特別な権限は必要ありません
- アプリがフォアグラウンドにあるときに動作します
一般的な使用例
Section titled “一般的な使用例”- デバッグ メニュー: デバイスをシェイクしたときに開発者向けオプションを表示します
- フィードバック: フィードバック フォームまたはバグ レポートをトリガーします。
- 更新: アプリのデータを更新するか、キャッシュをクリアします
- ゲーム: ゲーム制御メカニズムとしてシェイクを使用する
- 元に戻す: シェイクして元に戻す機能を実装します。
トラブルシューティング
Section titled “トラブルシューティング”シェイクが検出されませんでした:
- プラグインが
Shake.start()で開始されていることを確認してください - しきい値を調整してみてください (低いほど感度が高くなります)
- リスナーが正しく登録されていることを確認します
敏感すぎる/十分に敏感ではない:
start()オプションのthresholdパラメータを調整します- 値の範囲は通常、2.0 (非常に敏感) から 5.0 (あまり敏感ではない) です。