如何在iOS中实现轰轰烈烈的摇摆? [英] How to achieve vigorous Shake in iOS?
问题描述
我在ios中使用Shake Feature。它的工作正常,下面的代码。但当我进行剧烈摇晃时,它会发现震动并且在一秒钟内它会调用此行 if(histeresisExcited&&!L0AccelerationIsShaking(self.lastAcceleration,acceleration,0.2)){
虽然我继续摇晃。
histeresisExcited = NO;}
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.
如何实现剧烈震动?
我在这里做错了什么?
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 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
感谢您的帮助。
推荐答案
最后通过一点改变得到答案在我的代码中
Finally got the answer by little change in my 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);
}
到
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);
}
像Charm一样工作。
Works Like Charm.
希望它对像我这样的人有所帮助。
Hope it will help for some one like me.
这篇关于如何在iOS中实现轰轰烈烈的摇摆?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!