1

iOSでシェイク機能を使用しています。以下のコードで正常に動作します。しかし、激しくシェイクしようとすると、揺れを感知して、シェイクしif (histeresisExcited && !L0AccelerationIsShaking(self.lastAcceleration, acceleration, 0.2)) { histeresisExcited = NO;}続けても一瞬でこのラインを呼んでくれます。

激しい揺れを達成する方法は?

ここで何が間違っていますか?

// 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 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

ご協力いただきありがとうございます。

4

1 に答える 1

1

最後に、コードを少し変更して答えを得ました

以下のコードを変更する

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);

}

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);

}

チャームのように機能します。

私のような人に役立つことを願っています。

于 2012-09-05T08:44:11.553 に答える