- (void)startUpdateAccelerometer
{
/* 设置采样的频率,单位是秒 */
NSTimeInterval updateInterval = 0.05; // 每秒采样20次
// CGSize size = [self superview].frame.size;
// __block CGRect f = [self frame];
__block int stepCount = 0; // 步数
//在block中,只能使用weakSelf。
/* 判断是否加速度传感器可用,如果可用则继续 */
if ([motionManager isAccelerometerAvailable] == YES) {
/* 给采样频率赋值,单位是秒 */
[motionManager setAccelerometerUpdateInterval:updateInterval];
/* 加速度传感器开始采样,每次采样结果在block中处理 */
[motionManager startAccelerometerUpdatesToQueue:[NSOperationQueue currentQueue] withHandler:^(CMAccelerometerData *accelerometerData, NSError *error)
{
CGFloat sqrtValue =sqrt(accelerometerData.acceleration.x*accelerometerData.acceleration.x+accelerometerData.acceleration.y*accelerometerData.acceleration.y+accelerometerData.acceleration.z*accelerometerData.acceleration.z);
// 走路产生的震动率
if (sqrtValue > 1.552188 && valiadCountStep)
{
displayLink.paused = NO;
[Database save:TableLocalFoot entity:[NSDictionary dictionaryWithObjectsAndKeys:@"1",@"footid",[[NSUserDefaults standardUserDefaults] valueForKey:@"token"],@"userid",[NSDate date],@"time", nil]];
// [self.delegate totleNum:stepCount];
stepCount +=1;
valiadCountStep = NO;
}
}];
}
}