开发者

iPhone - core motion timestamp

开发者 https://www.devze.com 2023-02-12 23:10 出处:网络
I am using core motion on my app. I have the motionManager object assigned to a property on my main class. Something like

I am using core motion on my app. I have the motionManager object assigned to a property on my main class. Something like

@property (nonatomic, retain) CMMotionManager *motionManager;

every time I will use core motion, I assign it using something like:

- (void) initializeCoreMotion {
    CMMotionManager *myMotionManager = [[CMMotionManager alloc] init];
    self.motionManager = myMotionManager;
    [myMotionManager release];
}

Then, on the method that samples that data, I have this to read the timestamp of a sample.

CMDeviceMotion *motion = self.motionManager.deviceMotion;       
timestamp = motion.timestamp;
if (firstTime) {
  timestampReference = timestamp;
  firstTime = NO;
} else {
  timestamp = timestamp - timestampReference;
}

That is: the first time it samples, it stores the initial value. The subsequent times it samples, it will subtract the current value from the reference to know how many seconds passed.

the problem is this. Suppose I am sampling one time per second. I first sample for 10 seconds. So timestamp variable will go like 0, 1, 2, 3, 4, 5...10. Then I stop and do not sample for 20 seconds. When I start sampling again. The second time, timestamp will start at 31, 32, 33, 34...

I have che开发者_如何转开发cked the sampling method and firstTime is YES every time a first sample occurs...

any thoughts? How do I reset that?

thanks.


Seems to be fine the way you do it. But why do you create a new instance of CMMotionManager? I use:

if ([motionManager isDeviceMotionAvailable] && [motionManager isDeviceMotionActive]) {
    [motionManager stopDeviceMotionUpdates];
}

and to continue:

[motionManager startDeviceMotionUpdatesToQueue:operationQueue withHandler:deviceMotionHandler];

I was curious to see if there is still something wrong in iOS with the timestamps and thus I just tried out your code or similar thing using a static. Everything works as expected. Maybe your timestampReference is overwritten somewhere else?

0

精彩评论

暂无评论...
验证码 换一张
取 消