开发者

Displaying stop watch

In my app 开发者_JAVA百科I want to display stop watch kind of thing i.e when the user starts the app a atop watch should start and stops when the user presses the stop button. I am not sure as to how to use the NSTimer in this case.


Well, you know how to retrieve the start time ([NSDate date]) when the user clicks the button, so you just need to periodically retrieve the current time and display the difference between the start time and the current time to the user.

An NSTimer is used to periodically invoke a method. In that method you can just use [NSDate date] to retrieve the current time, then use the -[NSDate timeIntervalSinceDate:] method to figure out how much time has passed since the timer started. (That method will return the difference between the two dates in seconds, with sub-second precision. Then you just need to do some simple math to transform a number of raw seconds into a user-readable string (ie, 90 seconds => 1 min, 30 seconds) and display that in your UI.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜