Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>From my <a href="http://infinite-labs.net/diceshaker/" rel="noreferrer">Diceshaker</a> application:</p> <pre><code>// Ensures the shake is strong enough on at least two axes before declaring it a shake. // "Strong enough" means "greater than a client-supplied threshold" in G's. static BOOL L0AccelerationIsShaking(UIAcceleration* last, UIAcceleration* current, double threshold) { double deltaX = fabs(last.x - current.x), deltaY = fabs(last.y - current.y), deltaZ = fabs(last.z - current.z); return (deltaX &gt; threshold &amp;&amp; deltaY &gt; threshold) || (deltaX &gt; threshold &amp;&amp; deltaZ &gt; threshold) || (deltaY &gt; threshold &amp;&amp; deltaZ &gt; threshold); } @interface L0AppDelegate : NSObject &lt;UIApplicationDelegate&gt; { BOOL histeresisExcited; UIAcceleration* lastAcceleration; } @property(retain) UIAcceleration* lastAcceleration; @end @implementation L0AppDelegate - (void)applicationDidFinishLaunching:(UIApplication *)application { [UIAccelerometer sharedAccelerometer].delegate = self; } - (void) accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration { if (self.lastAcceleration) { if (!histeresisExcited &amp;&amp; L0AccelerationIsShaking(self.lastAcceleration, acceleration, 0.7)) { histeresisExcited = YES; /* SHAKE DETECTED. DO HERE WHAT YOU WANT. */ } else if (histeresisExcited &amp;&amp; !L0AccelerationIsShaking(self.lastAcceleration, acceleration, 0.2)) { histeresisExcited = NO; } } self.lastAcceleration = acceleration; } // and proper @synthesize and -dealloc boilerplate code @end </code></pre> <p>The histeresis prevents the shake event from triggering multiple times until the user stops the shake.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload