繁体   English   中英

时间间隔之间的倒数计时器iPhone

[英]Countdown timer between time interval iPhone

我需要iPhone识别当前时间(我可以使用NSDate来完成),并将其倒计时到下一个时间间隔,例如半小时中的每半小时。 例如:如果当前时间是2:12:30,间隔是每半小时,我希望倒数计时从17:30(还剩下17分30秒)开始,然后转到0。

欢迎使用代码,也欢迎一般的程序设计思想。 这是我开始倒数并获取当前时间的代码:

-(void)updateCounter:(NSTimer *)theTimer {
        if(secondsLeft > 0 ){
            secondsLeft -- ;
            minutes = (secondsLeft % 3600) / 60;
            seconds = (secondsLeft % 3600) % 60;
            waitingTimeLabel.text = [NSString stringWithFormat:@"%02d:%02d", minutes, seconds];
        }
        else{
            secondsLeft = 10;
        }
    }

    -(void)countdownTimer{
        //secondsLeft = minutes = seconds = 0;
        if([timer isValid])
        {
            [timer release];
        }
       // NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];  
        timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(updateCounter:) userInfo:nil repeats:YES];
        //[pool release];
    }
    -(NSDate *)getCurrentTime
    {
        NSDate *now = [NSDate date];

        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        dateFormatter.dateFormat = @"mm:ss";

        [dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
        NSLog(@"The Current Time is %@",[dateFormatter stringFromDate:now]);
        //[dateFormatter release];
        NSString *currentTime = [NSString stringWithFormat:@"%@",[dateFormatter stringFromDate:now]];
        currentTimeLabel.text = currentTime;
        return now;
    }
NSDate *now = [NSDate date];
int interval = 30*60; // half hour
long int nowSeconds = (long int) [now timeIntervalSince1970];
int secondsLeft = interval - (nowSeconds % interval); 
NSDate *nextIntervalDate =  [NSDate 
    dateWithTimeIntervalSince1970:nowSeconds+secondsLeft];

NSLog(@"Now is %@", now);
NSLog(@"The next interval point in time is %@", nextIntervalDate);
NSLog(@"That's another %d seconds (or %02d:%02d)", 
    secondsLeft, secondsLeft/60, secondsLeft%60);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM