我使用像
这样的touchbegin功能-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
{
touchStartTime = [event timestamp];
NSLog(@"Time of touch is %f ", touchStartTime);
}
但我想将其与
进行比较 double timeToCompare = [[NSDate date] timeIntervalSince1970];
但格式不兼容(时间戳有一些其他参考点)。如何将[event timestamp]
转换为普通NSDate,反之亦然?
答案 0 :(得分:0)
由于[事件时间戳]与系统运行时间有关,您可以获取[NSProcessInfo systemUptime]并同时获取当前系统时间并从那里开始。
[[NSDate date] timeIntervalSince1970] - [NSProcessInfo systemUptime] + [event timestamp] == [compareDate timeIntervalSince1970]
(好吧,我不会在血液中签名。我从未尝试过。)
答案 1 :(得分:0)
鉴于[UIEvent timestamp]
是自系统启动以来的秒数,有一种方法可以记录第一个事件的时间戳并将其用作参考。
@interface MyClass ()
{
BOOL _haveFirstTimestamp;
NSTimeInterval _firstTimestamp;
NSTimeInterval _firstTimestampTime;
}
@end
...
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
{
NSTimeInterval timestamp = [event timestamp];
if (!_haveFirstTimestamp) {
_haveFirstTimestamp = YES;
_firstTimestamp = timestamp;
_firstTimestampTime = [[NSDate date] timeIntervalSince1970];
}
NSTimestamp timeSince1970 = timestamp - _firstTimestamp + _firstTimestampTime;
NSLog(@"Time of touch is %f ", timeSince1970);
}