How to implement shake gestures in the Apple Watch app?

The WatchKit link does not seem to mention this. Did I miss something? Or is it really impossible to implement the shake gesture in the Apple Watch app?

The following is a typical implementation example of shake gestures in iOS:

// MARK: Gestures override func motionEnded(motion: UIEventSubtype, withEvent event: UIEvent) { if(event.subtype == UIEventSubtype.MotionShake) { // do something } } 
+6
source share
3 answers

No, in WatchKit you cannot do anything related to UIEvent with the current “remote interface” solution, where you can basically just talk about how to use the pre-configured interface from the storyboard and respond to actions such as clicking a button or line tables. According to Apple, next year there will be support for a much larger number of codes running on the watch.

Update: Now you can use native applications for watchOS 2. This feature may be present.

+4
source

Now it is possible in Watch OS2 with CMMotionManager part of CoreMotion.

You may have this workaround.

 let motionManager = CMMotionManager() if (motionManager.accelerometerAvailable) { let handler:CMAccelerometerHandler = {(data: CMAccelerometerData?, error: NSError?) -> Void in if (data!.acceleration.z > 0) { // User did shake } } motionManager.startAccelerometerUpdatesToQueue(NSOperationQueue.currentQueue()!, withHandler: handler) } 
+1
source

According to Roger, a workaround is to use CoreMotion.

You can use coreMotion for movement. I built this simple shell on my github.

0
source

Source: https://habr.com/ru/post/984488/


All Articles