iphone - 如何获得精确的时间,例如在 Objective-C 中以毫秒为单位?

有没有一种简单的方法可以非常精确地获取时间?

我需要计算方法调用之间的一些延迟。更具体地说,我想计算 UIScrollView 中的滚动速度。

最佳答案

NSDatetimeIntervalSince* 方法将返回一个 NSTimeInterval,它是具有亚毫秒精度的 double 。 NSTimeInterval 以秒为单位,但它使用 double 来提供更高的精度。

为了计算毫秒时间精度,你可以这样做:

// Get a current time for where you want to start measuring from
NSDate *date = [NSDate date];

// do work...

// Find elapsed time and convert to milliseconds
// Use (-) modifier to conversion since receiver is earlier than now
double timePassed_ms = [date timeIntervalSinceNow] * -1000.0;

Documentation on timeIntervalSinceNow .

还有许多其他方法可以使用 NSDate 计算此间隔,我建议您查看 NSDate Class Reference 中的 NSDate 类文档.

https://stackoverflow.com/questions/889380/

相关文章:

ios - 如何拦截 MKMapView 或 UIWebView 对象上的触摸事件?

objective-c - iPad 纵向和横向模式的尺寸调整类

ios - 带有固定部分标题的 UITableView

ios - 以编程方式检索 iPhone 上的内存使用情况

objective-c - iOS:比较两个日期

ios - 如何屏蔽 UIImageView?

objective-c - Swift 只读外部,读写内部属性

iphone - presentModalViewController :Animated is d

ios - 了解 NSRunLoop

objective-c - 获取对 iOS 应用程序中最顶层 View /窗口的引用