成人午夜激情影院,小视频免费在线观看,国产精品夜夜嗨,欧美日韩精品一区二区在线播放

iOS中使用NSTimer實現(xiàn)倒計時

2011-02-23 16:49:31來源:完美冠軍之家作者:

今天在CocoaChina上面看到有人在問倒計時怎么做,記得以前在看Iphone31天的時候做過一個,今天翻出來運行不了了,原因是我的Iphone SDK升級到3.1了,以前使用的是2.2.1,在2.2.1里面是可以使用NSCalendarDate的,但

今天在CocoaChina上面看到有人在問倒計時怎么做,記得以前在看Iphone31天的時候做過一個,今天翻出來運行不了了,原因是我的Iphone SDK升級到3.1了,以前使用的是2.2.1,在2.2.1里面是可以使用NSCalendarDate的,但是在3.1里面不能夠使用,怎么辦,只好用NSTimer了,最后還是給實現(xiàn)了。代碼也比較簡單,開始運行viewDidLoad的時候加載 [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerFireMethod:) userInfo:nil repeats:YES];//使用timer定時,每秒觸發(fā)一次,然后就是寫selector了。
 
- (void)timerFireMethod:(NSTimer*)theTimer
{
 //NSDateFormatter *dateformatter = [[[NSDateFormatter alloc]init] autorelease];//定義NSDateFormatter用來顯示格式
 //[dateformatter setDateFormat:@"yyyy MM dd hh mm ss"];//設定格式
 NSCalendar *cal = [NSCalendar currentCalendar];//定義一個NSCalendar對象
 NSDateComponents *shibo = [[NSDateComponents alloc] init];//初始化目標時間(好像是世博會的日期)
 [shibo setYear:2010];
 [shibo setMonth:5];
 [shibo setDay:1];使用NSTimer實現(xiàn)倒計時
 [shibo setHour:8];
 [shibo setMinute:0];
 [shibo setSecond:0];
 
 NSDate *todate = [cal dateFromComponents:shibo];//把目標時間裝載入date
 [shibo release];
// NSString *ssss = [dateformatter stringFromDate:dd];
// NSLog([NSString stringWithFormat:@"shibo shi:%@",ssss]);
 
 NSDate *today = [NSDate date];//得到當前時間
// NSString *sss = [dateformatter stringFromDate:today];
// NSLog([NSString stringWithFormat:@"xianzai shi:%@",sss]);
 //用來得到具體的時差
 unsigned int unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
 NSDateComponents *d = [cal components:unitFlags fromDate:today toDate:todate options:0];
 lab.text = [NSString stringWithFormat:@"%d年%d月%d日%d時%d分%d秒",[d year],[d month], [d day], [d hour], [d minute], [d second]];
}

\

這樣就實現(xiàn)了倒計時的功能。

原文:http://blog.sina.com.cn/s/blog_60b45f230100f3v2.html
關(guān)鍵詞:iOSNSTimer倒計時

贊助商鏈接:

主站蜘蛛池模板: 涪陵区| 许昌县| 德格县| 牡丹江市| 泸定县| 平塘县| 晋中市| 休宁县| 滨海县| 乌兰浩特市| 金秀| 报价| 祁东县| 上栗县| 澎湖县| 米易县| 伊吾县| 霍山县| 广饶县| 白银市| 诏安县| 庆阳市| 易门县| 乌鲁木齐县| 万盛区| 衡山县| 九江县| 新疆| 威海市| 江西省| 金昌市| 樟树市| 碌曲县| 孙吴县| 新和县| 化德县| 田林县| 攀枝花市| 涡阳县| 仁化县| 刚察县|