
- (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是如何工作的.
解决方法 当显示器关闭时,您无法访问加速度计,但即使显示屏关闭,您也可以监控位置更改,无论iPhone型号如何.根据我的猜测,Moves应用程序使用基于地理位置的更改来计算步数.在CLLocationmanger文档中,我读到了这个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
以上是内存溢出为你收集整理的使用iOS 7获取后台步骤全部内容,希望文章能够帮你解决使用iOS 7获取后台步骤所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)