1
A
回答
4
你似乎想要2件事。
一個逝去的問題的開始之間的時間,直到它得到答案,你可以做到這一點:
NSDate *now = [NSDate date];
NSTimeInterval *elapsedTime = [now timeIntervalSinceNow];
二是使用NSTimer
進行倒計時。你可以用下面的代碼做到這一點:
NSTimer *theTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(countdownTracker:) userInfo:nil repeats:YES];
// Assume a there's a property timer that will retain the created timer for future reference.
self.timer = theTimer;
// Assume there's a property counter that track the number of seconds before count down reach.
self.counter = 10;
將由計時器調用的方法。
- (void)countdownTracker:(NSTimer *)theTimer {
self.counter--;
if (self.counter < 0) {
[self.timer invalidate];
self.timer = nil;
self.counter = 0;
[[[[UIAlertView alloc] initWithTitle:@"Countdown" message:@"Countdown completed." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] autorelease] show];
}
}
這應該有效。
1
- (void) showClock
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterNoStyle];
[dateFormatter setTimeStyle:NSDateFormatterShortStyle];
[dateFormatter setDateFormat:@"HH"];
int hour = [[dateFormatter stringFromDate:[NSDate date]] intValue];
[dateFormatter setDateFormat:@"mm"];
int minute = [[dateFormatter stringFromDate:[NSDate date]] intValue];
[dateFormatter release];
NSTimeInterval remainingSec = [databaseDate timeIntervalSinceNow];
if (remainingSec >= 0)
{
timer = nil;
self.databaseDate = [NSDate dateWithTimeIntervalSinceNow:0];
remainingSec = [databaseDate timeIntervalSinceNow];
timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self
selector:@selector(showClock)
userInfo:nil
repeats:YES];
}
NSInteger hours = -remainingSec/3600;
NSInteger remainder = ((NSInteger)remainingSec)% 3600;
NSInteger minutes = -remainder/60;
NSInteger seconds = -remainder % 60;
//NSString * stringvalue=[[NSString alloc]initWithFormat:@"%02d:%02d:%02d", hours, minutes, seconds];
int hoursvalue=hour+hours;
int minutsvalue=minute+minutes;
stopWatchLabel.text=[NSString stringWithFormat:@"%i:%i",hoursvalue,minutsvalue];
}
相關問題
- 1. NSTimer Lag - iPhone SDK
- 2. 如何在iphone sdk中同步音頻與NStimer?
- 3. 在Linux上設置iPhone SDK?
- 4. Iphone sdk - 如何設置'模板'
- 5. 在iPhone SDK中設置鈴聲
- 6. 如何設置NSTimer從活動實例的目標iphone
- 7. 如何在Iphone SDK中將Repeatinterval設置爲兩分鐘?
- 8. 如何在iphone中設置zooz框架的方向sdk
- 9. 如何在iPhone中爲Tabbar項設置圖像sdk
- 10. 如何在iPhone中爲UIWebView設置背景顏色sdk
- 11. 如何在iphone sdk中設置JSON數組?
- 12. 與NStimer iphone sdk同步聲音?
- 13. Iphone SDK設置靜態IP
- 14. iPhone SDK修改WiFi設置
- 15. PSMultiValueSpecifier - iPhone SDK +設置包
- 16. 如何設置報警應用的NSTimer?
- 17. 在iPhone SDK中設置一個基於iPhone的Web界面
- 18. 如何在iPhone SDK
- 19. 如何在iPhone SDK
- 20. 如何在iPhone SDK
- 21. 如何在aws php sdk中設置DeleteOnTermination?
- 22. 如何在Android SDK中設置AdWhirl?
- 23. 如何爲iPhone SDK中的速度計指針設置動畫?
- 24. 如何設置圖像在iPhone SDK的方向
- 25. 如何在iPhone上設置一個不可觸摸的uitableviewcell sdk
- 26. 如何在一臺Mac上設置2個iPhone SDK
- 27. 在Swift中使用PageViewController設置NSTimer
- 28. 在異步請求中設置NSTimer
- 29. 在iPhone SDK中設置前置攝像頭
- 30. 如何設置iPhone
將它在後臺運行? – UserDev 2014-01-24 14:03:15