iphone - 如何在iOS中实现剧烈的Shake?

标签 iphone ios ios5 shake

我在 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

感谢您的帮助。

最佳答案

通过稍微修改我的代码终于得到了答案

修改下面的代码

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

像魅力一样工作。

希望对像我这样的人有所帮助。

关于iphone - 如何在iOS中实现剧烈的Shake?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12246029/

相关文章:

iphone - 如何为 UIButton 的文本添加换行符?

ios - Siri Intent Extension 内的 TouchID 身份验证

audio - 使用 MPMoviePlayerController 和 iOS5 在 iPad 上没有音频,可在模拟器中使用

iphone - Sencha Touch 和保存本地媒体

iphone - 如何给图像赋值

ios - 如何在 Objective-C 中使用多个参数调用 Swift 初始化方法

ios - 如何使 UIToolbar 出现在 Storyboard 中以进行可视化编辑

ios - 在 UiButton 中对齐图像和标题

iphone - 如何理解 iTunes Connect 中的应用程序下载?

ios - 在 Swift 中测试 NSString 是否为数字的扩展