Shake Detection

*Note: I started this venture on the community slack, but due to the limitations of free accounts, I’ve decided to share my discoveries here so that they don’t disappear in the future. Thank you to @bolav @uldis and for all their guidance on how to go about solving this challenge. *

This is a work in progress and I plan to update this thread as I learn learn how to implement this be the through experimentation and community feedback.

In order to detect a shake gesture I’ll need to leverage the Core Motion framework, but the challenge is that to utilize this we need to overwrite canBecomeFirstResponder returning YES and implement the motionBegan:withEvent defined in UIResponsder, the the superclass for the both AppDelegate and UIViewController classes. Fortunately in a fuse app, the AppDelegate is actually extends the UIViewController class, so in order be minimally invasive with my solution, I chose to create a new class FuseAppDelegate which also extends the UIViewController:

![file](https://s3.us-east-2.amazonaws.com/fuse-legacy-forum-assets/OGA1sKTFeHxN-image-1472624883029.27.46 PM :crescent_moon:.png)

So now I can just change the base class in the generated AppDelegate, like so:

![file](https://s3.us-east-2.amazonaws.com/fuse-legacy-forum-assets/u9cIGdFMirBf-image-1472624955838.08.38 PM :crescent_moon:.png)

Later this step can be implemented with some sort of build script.

In this class I’ll implement the necessary methods to detect a shake gesture natively, but I’ll get to that part later. First I need to create an .uno module that will be converted by fusion when the project is built. I created a Shaker.uno file in my project:

![file](https://s3.us-east-2.amazonaws.com/fuse-legacy-forum-assets/3JeYoDcpdIWp-image-1472625167491.04.55 PM :crescent_moon:.png)

This module creates a simple singleton with method ShakeDetected(). This part took a while to figure out, repeatedly regenerating the project and figuring out how to how to access them on the other side. Finally I arrived at the solution of using a singleton, because it made invoking the method from Objective-C more straight forward and I’m a noob to using Uno. Here are the details of my FuseAppDelegate.mm file:

![file](https://s3.us-east-2.amazonaws.com/fuse-legacy-forum-assets/UIWA69K6TkHZ-image-1472625515433.06.55 PM :crescent_moon:.png)

That interesting line @ 37 is how I invoke the method generated from Uno to c++ source. It works though, so I’m not complaining. Here’s what I did to capture the event on the fuse side within a set of JavaScript tags.

![file](https://s3.us-east-2.amazonaws.com/fuse-legacy-forum-assets/B7fLm12hFe7j-image-1472625803789.42.47 PM :crescent_moon:.png)

And the results when I shake connected device is a success!

![file](https://s3.us-east-2.amazonaws.com/fuse-legacy-forum-assets/q5bboHummwy8-image-1472625909694.43.04 PM :crescent_moon:.png)

Again, this is a very rough experimentation and hope to refine the solution over time, posting discoveries here.

Nice work :slight_smile:

Thank you!

Cool! You can use a macro to call back to Uno from Objective-C, provided your file is included as filename.mm:ObjCSource in the unoproj. The macro would look something like @{Shaker.ShakeDetected():Call()}. See the docs for more info. :slight_smile:

It will be rad if a plugin is available for this. I would really love to use it in a current project.

Nice work! Is there any change to see the code?