我正在做一个POC,看看我是否能找到摇晃的强度。
class ViewController: UIViewController {
override func motionBegan(motion: UIEventSubtype, withEvent event: UIEvent) {
println("started shaking!")
}
override func motionEnded(motion: UIEventSubtype, withEvent event: UIEvent) {
println("ended shaking!")
}
}
我看不到有什么能告诉我抖动的剧烈程度。就我而言,这种震动可能是持续几秒钟的事件。
最佳答案
这是我放入didAccelerate回调中的代码,其中包含一些类变量和常量:
UIAccelerationValue accelX, accelY, accelZ;
#define kAccelerometerFrequency 25 //Hz
#define kFilteringFactor 0.1
#define kMinShakeInterval 0.1
#define kShakeAccelerationThreshold 0.2
-(CMMotionManager*) motionManager{
if (_motionManager==nil) {
_motionManager=[[CMMotionManager alloc] init];
_motionManager.accelerometerUpdateInterval=1.0/25;
_motionManager.gyroUpdateInterval=1.0/25;
}
return _motionManager;
}
-(void) viewDidLoad{
[...]
[[self motionManager] startAccelerometerUpdatesToQueue:[NSOperationQueue currentQueue]
withHandler:^(CMAccelerometerData *accelerometerData, NSError *error) {
[self didAccelerate:accelerometerData.acceleration];
if(error){
NSLog(@"%@", error);
}
}];
[...]
}
- (void)didAccelerate:(CMAcceleration)acceleration{
UIAccelerationValue lenght, x, y, z;
accelX=acceleration.x*kFilteringFactor + accelX * (1.0 - kFilteringFactor);
accelY=acceleration.y*kFilteringFactor + accelY * (1.0 - kFilteringFactor);
accelZ=acceleration.z*kFilteringFactor + accelZ * (1.0 - kFilteringFactor);
x=acceleration.x - accelX;
y=acceleration.y - accelY;
z=acceleration.z - accelZ;
lenght=sqrt(x*x + y*y + z*z);
if (lenght>=kShakeAccelerationThreshold && (CFAbsoluteTimeGetCurrent()>lastTime + kMinShakeInterval)){
//execute shaking actions on main thread
}
}
关于ios - 有没有办法提高震动的强度?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30058947/