WatchShaker is a lightweight library for detecting shakes and wrist movements on Apple Watch. This library offers a simple and efficient way to detect common user gestures on the watch.
Features
- Shake detection: Detect when the user shakes their Apple Watch.
- Wrist flick detection: Detect when the user flicks their wrist.
- Customizable sensitivity: Adjust the sensitivity level for detecting shakes and wrist flicks.
- Delegate methods: Use delegate methods to receive notifications for shake and flick events.
- Support for complications: Enable WatchShaker to work with complications on Apple Watch.
- Easy integration: Seamlessly integrate WatchShaker into your existing Apple Watch apps.
Installation
To install WatchShaker, follow these steps:
- Download the WatchShaker framework from the official GitHub repository: https://github.com/your-link-goes-here
- Add the WatchShaker framework to your Xcode project.
- Import the WatchShaker module in your Swift file:
import WatchShaker
- Set up the WatchShaker class as shown in the example below:
import WatchKit
// Your Watch Extension Delegate
class ExtensionDelegate: NSObject, WKExtensionDelegate {
var watchShaker: WatchShaker?
func applicationDidFinishLaunching() {
watchShaker = WatchShaker(delegate: self)
watchShaker?.start()
}
// Implement the delegate methods
}
// Your Shake Delegate
extension ExtensionDelegate: WatchShakerDelegate {
func watchShakerDidShake(device: WKInterfaceDevice) {
// Action to perform on shake event
}
func watchShakerDidFlick(device: WKInterfaceDevice) {
// Action to perform on wrist flick event
}
}
Note: Make sure to implement the shake and flick delegate methods in your code to receive the corresponding events.
Usage
Using WatchShaker is straightforward. Here’s an example of how you can use it in your Apple Watch app:
import WatchShaker
class InterfaceController: WKInterfaceController {
var watchShaker: WatchShaker?
override func awake(withContext context: Any?) {
super.awake(withContext: context)
watchShaker = WatchShaker(delegate: self)
watchShaker?.start()
}
// Implement the delegate methods
}
extension InterfaceController: WatchShakerDelegate {
func watchShakerDidShake(device: WKInterfaceDevice) {
// Action to perform on shake event
}
func watchShakerDidFlick(device: WKInterfaceDevice) {
// Action to perform on wrist flick event
}
}
Customization
WatchShaker allows customization of sensitivity levels for shake and flick events. To customize the sensitivity, use the following methods:
import WatchShaker
class InterfaceController: WKInterfaceController {
var watchShaker: WatchShaker?
override func awake(withContext context: Any?) {
super.awake(withContext: context)
// Set custom sensitivity levels
WatchShaker.shared.shakeThreshold = 2.5
WatchShaker.shared.flickThreshold = 2.0
// Start WatchShaker as usual
watchShaker = WatchShaker(delegate: self)
watchShaker?.start()
}
// Implement the delegate methods
}
Complication Support
WatchShaker can be used alongside complications in your Apple Watch app. To enable complications support, follow these steps:
- In your project target, go to the “Capabilities” tab.
- Enable the “Background Modes” capability.
- Check the “Audio, AirPlay and Picture in Picture” checkbox.
Note: Enabling background modes allows WatchShaker to run in the background and continue detecting shakes and flicks even when the app is in the background.
Conclusion
WatchShaker simplifies shake and wrist flick detection on Apple Watch, providing an efficient way to add gesture-based interactions in your watchOS apps. With its customizable sensitivity levels and delegate methods, you can easily integrate WatchShaker into your projects. Don’t miss the opportunity to enhance user experiences on Apple Watch with WatchShaker!