- (void)countSteps { [[UIAccelerometer sharedAccelerometer] setUpdateInterval:1.0 / KUPDATEFREQUENCY]; [[UIAccelerometer sharedAccelerometer] setDelegate:self]; px = py = pz = 0; numSteps = 0; self.labelSteps.text = [NSString stringWithFormat:@"%d",numSteps]; } - (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration { float xx = acceleration.x; float yy = acceleration.y; float zz = acceleration.z; float dot = (px * xx) + (py * yy) + (pz * zz); float a = ABS(sqrt(px * px + py * py + pz * pz)); float b = ABS(sqrt(xx * xx + yy * yy + zz * zz)); dot /= (a * b); if (dot <= 0.82) { if (!isSleeping) { isSleeping = YES; [self performSelector:@selector(wakeUp) withObject:nil afterDelay:0.3]; numSteps += 1; self.labelSteps.text = [NSString stringWithFormat:@"%d",numSteps]; } } px = xx; py = yy; pz = zz; } - (void)wakeUp { isSleeping = NO; }
使用此代码,当iPhone的显示器打开时,它工作得很好,但是当我关闭显示器时,它不再起作用.为了跟踪位置,我在iOS 7中看到了一个功能“后台模式”.使用此功能,我可以在iPhone显示屏关闭时获取坐标.现在我要在显示器关闭时获取加速度计值,我该怎么做?我在网上看到iOS不允许在后台模式下使用加速度计(只有iPhone 5s和协处理器M7可以在显示器关闭时获得加速度计值),我如何在后台模式下使用加速度计来计算步数?我想应该有一种方法,否则我无法理解Moves app是如何工作的.
解决方法
In iOS 6 and later,you can defer the delivery of location data when
your app is in the background. It is recommended that you use this
feature in situations where your app could process the data later
without any problems. For example,an app that tracks the user’s
location on a hiking trail could defer updates until the user hikes a
certain distance and then process the points all at once. Deferring
updates helps save power by allowing your app to remain asleep for
longer periods of time.
- (void)allowDeferredLocationUpdatesUntilTraveled:(CLLocationDistance)distance timeout:(NSTimeInterval)timeout
If your app is in the background and the system is able to optimize
its power usage,the location manager tells the GPS hardware to store
new locations internally until the specified distance or timeout
conditions are met. When one or both criteria are met,the location
manager ends deferred locations by calling the
locationManager:didFinishDeferredUpdatesWithError: method of its
delegate and delivers the cached locations to the
locationManager:didUpdateLocations: method. If your app is in the
foreground,the location manager does not defer the deliver of events
but does monitor for the specified criteria. If your app moves to the
background before the criteria are met,the location manager may begin
deferring the delivery of events.
因此,当您获得一组缓存位置时,您可以计算出发生的大致步骤数.我只是给你所有狂野的想法,由你决定!
看到这一点,似乎很有趣
https://www.cocoacontrols.com/controls/somotiondetector