如何在xcode中的标签上反向显示定时器?

时间:2015-12-11 12:23:14

标签: ios objective-c

我有一个视图控制器,我想以相反的顺序显示时间。我的默认时间是00:59:59?我想要这样的输出(00:58:00,00:57:00,00:56:00,00:30:00)?

  int currentTimeInSeconds;
  NSTimer *myTimer;
if (!timeLabel)
{
    timeLabel =0;
}
if (!myTimer)
{
    myTimer = [self createTimer];
}
-(NSTimer *)createTimer
{
   return [NSTimer scheduledTimerWithTimeInterval:1.0 target:self  selector:@selector(timerTicked) userInfo:nil repeats:YES];
}
-(void)timerTicked
{
    if (currentTimeInSeconds == 0)
    {
        [myTimer invalidate];
        currentTimeInSeconds = 60;
        timeLabel.text = [self formattedTime:currentTimeInSeconds];
    }
    else
    {
        currentTimeInSeconds--;
        self.timeLabel.text = [self formattedTime:currentTimeInSeconds];
    }
}
-(NSString *)formattedTime:(int)totalSeconds
{
     int seconds = totalSeconds%60;
     int minutes = (totalSeconds/60)%60;
     int hours = totalSeconds/3600;
     return [NSString stringWithFormat:@" %02d:%02d:%02d",hours,minutes,seconds];
}

2 个答案:

答案 0 :(得分:3)

- (IBAction)btnStart:(id)sender {

    timet=[self.txtTime.text integerValue];
    self.lblTime.text=[NSString stringWithFormat:@"%lu", (unsigned long)timet];
    [NSTimer scheduledTimerWithTimeInterval:timet
                                     target:self
                                   selector:@selector(targetMethod)
                                   userInfo:nil
                                    repeats:NO];
    [self time];

}
-(void)time
{
    [NSTimer scheduledTimerWithTimeInterval:1
                                    target:self
                                    selector:@selector(targetMethod2)
                                    userInfo:nil
                                    repeats:NO];
}
-(void)targetMethod
{
    NSLog(@"STOP");
    UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Stop" message:@"Time is over" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];//, nil
    [alert show];
}
-(void)targetMethod2
{

    if(timet!=0)
    {
        timet-=1;
        NSLog(@"%lu",(unsigned long)timet);
        self.lblTime.text=[NSString stringWithFormat:@"%lu",(unsigned long)timet];

        [self time];
    }
}
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0)
    {
        self.txtTime.text=nil;
        self.lblTime.text=nil;

    }
}

----------------第二个代码---------------------

@interface ViewController ()
{
    UILabel *progress;
    NSTimer *timer;
    int currMinute;
    int currSeconds;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    progress=[[UILabel alloc] initWithFrame:CGRectMake(80, 15, 100, 50)];
    progress.textColor=[UIColor redColor];
    [progress setText:@"Time : 3:00"];
    progress.backgroundColor=[UIColor clearColor];
    [self.view addSubview:progress];
    currMinute=3;
    currSeconds=00;
    [self start];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)start
{
    timer=[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerFired) userInfo:nil repeats:YES];

}
-(void)timerFired
{
    if((currMinute>0 || currSeconds>=0) && currMinute>=0)
    {
        if(currSeconds==0)
        {
            currMinute-=1;
            currSeconds=59;
        }
        else if(currSeconds>0)
        {
            currSeconds-=1;
        }
        if(currMinute>-1)
            [progress setText:[NSString stringWithFormat:@"%@%d%@%02d",@"Time : ",currMinute,@":",currSeconds]];
    }
    else
    {
        [timer invalidate];
    }
}

答案 1 :(得分:0)

- (IBAction)startCountdown:(id)sender {

    //Set up a timer that calls the updateTime method every second to update the label
    NSTimer *timer;
    timer = [NSTimer scheduledTimerWithTimeInterval:1.0
                                             target:self
                                           selector:@selector(updateCounterLabel)
                                           userInfo:nil
                                            repeats:YES];
}

每1秒调用一次函数。

   -(void)timerTicked
{

    NSString *dateString = @"12-12-2015";
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

    [dateFormatter setDateFormat:@"dd-MM-yyyy"];
    NSDate *dateFromString = [[NSDate alloc] init];

    dateFromString = [dateFormatter dateFromString:dateString];

    NSDate *now = [NSDate date];
    NSCalendar *calendar = [NSCalendar currentCalendar];

    NSDateComponents *componentsHours = [calendar components:NSHourCalendarUnit fromDate:now];
    NSDateComponents *componentMint = [calendar components:NSMinuteCalendarUnit fromDate:now];
    NSDateComponents *componentSec = [calendar components:NSSecondCalendarUnit fromDate:now];

    NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *componentsDaysDiff = [gregorianCalendar components:NSDayCalendarUnit
                                                                fromDate:now
                                                                  toDate:dateFromString
                                                                 options:0];

    long day = (long)componentsDaysDiff.day;
    long hours = (long)(24-componentsHours.hour);
    long minutes = (long)(60-componentMint.minute);
    long sec = (long)(60-componentSec.second);

    NSString *strForday = [NSString stringWithFormat:@"%02ld",day];
    NSString *strForhours = [NSString stringWithFormat:@"%02ld",hours];
    NSString *strForminutes = [NSString stringWithFormat:@"%02ld",minutes];
    NSString *strForsec = [NSString stringWithFormat:@"%02ld",sec];
    NSLog(@"%@ %@ %@ %@ ",strForday,strForhours,strForminutes,strForsec);

    if (day == 0 && hours == 0 && minutes == 0 && sec == 0) {
        [myTimer invalidate];
    }
}