Getting Started
此内容尚不支持你的语言。
-
Install the package
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 -
Sync with native projects
Terminal window npx cap syncTerminal window pnpm cap syncTerminal window yarn cap syncTerminal window bunx cap sync -
Configure the plugin
Basic Usage Example:
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});Sensitivity Configuration:
// Configure shake sensitivityawait Shake.start({threshold: 3.5 // Adjust sensitivity (default: 3.5)});No additional setup required for iOS.
No additional setup required for Android.
-
Stop listening for shakes
// Stop shake detection when not neededawait Shake.stop();// Remove specific listenerconst handle = await Shake.addListener('shake', () => {console.log('Shaken!');});handle.remove(); -
Example implementation
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 Reference
Methods
start(options?: ShakeOptions)
Start listening for shake gestures.
Parameters:
options
(optional): Configuration objectthreshold
: number - Sensitivity threshold (default: 3.5)
stop()
Stop listening for shake gestures.
addListener('shake', callback: () => void)
Add a listener for shake events.
Returns: Promise with a handle to remove the listener
Interfaces
interface ShakeOptions { threshold?: number; // Shake sensitivity threshold}
Platform Notes
iOS
- Uses the device’s accelerometer to detect shake gestures
- Works in both foreground and background (if app has background permissions)
Android
- Uses the SensorManager to detect shake events
- Requires no special permissions
- Works when app is in foreground
Common Use Cases
- Debug Menu: Show developer options when device is shaken
- Feedback: Trigger feedback form or bug report
- Refresh: Refresh app data or clear cache
- Games: Use shake as a game control mechanism
- Undo: Implement shake-to-undo functionality
Troubleshooting
Shake not detected:
- Ensure the plugin is started with
Shake.start()
- Try adjusting the threshold value (lower = more sensitive)
- Check that listeners are properly registered
Too sensitive/not sensitive enough:
- Adjust the
threshold
parameter instart()
options - Values typically range from 2.0 (very sensitive) to 5.0 (less sensitive)