如何在iOS中实现轰轰烈烈的摇摆?
我在ios中使用Shake Feature。 它的工作正常,下面的代码。 但是当我进行剧烈摇晃时,它会发现震动,并且在一秒钟内它会调用此行, 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 { 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);
}
像魅力一样工作。
希望它对像我这样的人有所帮助。
- SpriteKit:在场景之间传递数据
- 在Objective-C XCode中将常量链接到pch时出错
- UIWebView不会加载URL
- 主线程出错:MKNormalizedPointForLayer
- 为什么我的UITableView不响应touchesBegan?
- 可以使用ABNewPersonViewController编辑现有记录吗?
- 当用户按下button显示dateselect器视图和selectdate,然后显示为button标题和进一步closuresdateselect器视图
- 如何访问UINavigationController中的堆栈
- Objective-C代码生成给定文件和目录的相对path