Question

I am using Shake Feature in ios. Its working fine with the below code. But when i go for vigorous shake, it detected the shaking and with in a second it calls this line if (histeresisExcited && !L0AccelerationIsShaking(self.lastAcceleration, acceleration, 0.2)) { histeresisExcited = NO;} though i keep on shaking.

How to achieve vigorous shaking?

What i am doing wrong here?

// 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 > threshold && deltaY > threshold) ||
            (deltaX > threshold && deltaZ > threshold) ||
            (deltaY > threshold && deltaZ > threshold);
}

@interface L0AppDelegate : NSObject <UIApplicationDelegate> {
    BOOL histeresisExcited;
    UIAcceleration* lastAcceleration;
}

@property(retain) UIAcceleration* lastAcceleration;

@end

Implementation

@implementation L0AppDelegate

- (void)applicationDidFinishLaunching:(UIApplication *)application {
     [UIAccelerometer sharedAccelerometer].delegate = self;
}

- (void) accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {

    if (self.lastAcceleration) {
            if (!histeresisExcited && L0AccelerationIsShaking(self.lastAcceleration, acceleration, 0.7)) {
                    histeresisExcited = YES;

                    /* SHAKE DETECTED. DOING SOME DATABASE OPERATIONS HERE. */

            } else if (histeresisExcited && !L0AccelerationIsShaking(self.lastAcceleration, acceleration, 0.2)) {
                    histeresisExcited = NO;

                 /* SHAKE STOPPED. CALLING A VIEW CONTROLLER TO DISPLAY THE CONTENTS GOT FROM THE DATABASE*/
            }
    }

    self.lastAcceleration = acceleration;
 }

// and proper @synthesize and -dealloc boilerplate code

 @end

Thanks for your help.

Was it helpful?

Solution

Finally got the answer by little change in my code

Changing the below code

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 > threshold && deltaY > threshold) ||
        (deltaX > threshold && deltaZ > threshold) ||
        (deltaY > threshold && deltaZ > threshold);

}

To

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 > threshold) ||
        (deltaY > threshold) ||
        (deltaZ > threshold);

}

Works Like Charm.

Hope it will help for some one like me.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top