Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Implement shake gestures in an Apple Watch application?

The WatchKit reference seems to make no mention about it. Have I missed something? Or is it really not possible to implement a shake gesture in an Apple Watch application?

The following is a typical example of a shake gesture implementation on iOS:

// MARK: Gestures
override func motionEnded(motion: UIEventSubtype, withEvent event: UIEvent) {
    if(event.subtype == UIEventSubtype.MotionShake) {
        // do something
    }
}
like image 924
fabian Avatar asked Mar 31 '15 09:03

fabian


3 Answers

No, it is not possible to do anything having to do with UIEvents in WatchKit right now, with the current solution's "remoted UI" approach where you mostly just get to tell the watch how to use the pre-arranged UI from the storyboard and react to actions like tapping a button or a table row. There will be support for a lot more code running on the watch later this year, according to Apple.

Update: Native apps are now possible for watchOS 2. This functionality may be present.

like image 91
Jesper Avatar answered Nov 15 '22 00:11

Jesper


This is now possible in Watch OS2 with CMMotionManager a part of CoreMotion.

You can 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)
}
like image 23
Roger Avatar answered Nov 14 '22 23:11

Roger


How Roger say, an workaround is use the CoreMotion.

You can use coreMotion to get movement. I build this simple wrapper on my Github.

like image 27
Ezequiel França Avatar answered Nov 14 '22 22:11

Ezequiel França