3:30 分钟的定时器有添加和跳过选项?

timer for 3:30 mins which has a options with add and skip?

我是 ios 的新手,所以我需要帮助制作一个 3:30 分钟的计时器,它有两个选项,添加和跳过,在计时器达到 [=17= 后消失].请帮助我,我现在处境非常困难。

这是定时器

-(void)updatelable:(NSTimer *)timer {

    remainingTime=180;

    for (int i=remainingTime;i==0 ;i--) {

        NSInteger minutes = floor(*(remainingTime))/ 60;
        remainingTime = remainingTime-(minutes*60);

        NSInteger seconds = remainingTime;

        second.text =[NSString stringWithFormat:@"%02ld",seconds];
        minute.text =[NSString stringWithFormat:@"%02ld",minutes];

        timerLabel.text = @"Time up!!";
    }

    [timer invalidate];
}

试试这个

.h 文件

{
    int counter;
    NSTimer timer;
}

在 .m 文件中

 - (void)viewDidLoad
 {
     counter = 210;//total Time
     timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(targetMethod:) userInfo:nil repeats:YES];        
 }


-(void)targetMethod:(NSTimer*)timr
{
     if(counter == 0)
     {
         [timer invalidate]
         //set label hidden here after 3:30 minutes
     }
     //strTime is string containing remaining time
     NSString *strTime = [self formatTimeFromSeconds:counter];
     counter--;
}

对于标签文本调用此方法

 -(NSString *)formatTimeFromSeconds:(int)numberOfSeconds
{
    int seconds = numberOfSeconds % 60;
    int minutes = (numberOfSeconds / 60) % 60;
    int hours = numberOfSeconds / 3600;
    //we have >=1 hour => example : 3h:25m
    if (hours)
        return [NSString stringWithFormat:@"%dh:%dm:%ds", hours, minutes, seconds];
    //we have 0 hours and >=1 minutes => example : 3m:25s
    if (minutes)
        return [NSString stringWithFormat:@"%dm:%ds", minutes, seconds];
    //we have only seconds example : 25s
    return [NSString stringWithFormat:@"%ds", seconds];
}

这不是最复杂的方法,但它确实适合您,并且准确地展示了这种方法在实践中应该如何工作:

static NSInteger remainingTime = 210;

// ...

- (void)startTimer {
    [[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(counter:) userInfo:nil repeats:TRUE] fire];
}

- (void)counter:(NSTimer *)timer {
    remainingTime--;

    NSLog(@"%02ld:%02ld", (NSInteger)(remainingTime / 60.0), (remainingTime % 60));

    if (remainingTime == 0) {
        // times up!
       [timer invalidate];
    }
}
-(void)viewDidLoad{
 counter =10;//total Time
    timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(targetMethod:) userInfo:nil repeats:YES];
 }
-(void)targetMethod:(NSTimer*)timr
{
if(counter == 0)
{
    [timer invalidate];


    //set label hidden here after 3:30 minutes



     [second setHidden:YES];
     [minute setHidden:YES];
     [colon setHidden:YES];
     [skip setHidden:YES];
}

NSInteger minutes = counter/ 60;
NSInteger seconds = counter-(minutes*60);


second.text =[NSString stringWithFormat:@"%02ld",seconds];
minute.text =[NSString stringWithFormat:@"%02ld",minutes];
counter--;
}