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]( PM :crescent_moon:.png)

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

![file]( 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 file in my project:

![file]( 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 file:

![file]( 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]( PM :crescent_moon:.png)

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

![file]( 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 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?