-->

如何实现iOS中蓬勃摇?(How to achieve vigorous Shake in iOS?

2019-09-22 01:59发布

我正在使用的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

谢谢你的帮助。

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

}

作品般的魅力。

希望这将有助于对一些人喜欢我。



文章来源: How to achieve vigorous Shake in iOS?