Shake Detection

Back to Show and Tell

*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

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

file

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

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

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

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

file

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

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. :)

Post Stats
  • 5
    replies
  • 1587
    views
  • 5
    users
Frequent Posters