多个计时器Objective-C

用户名

我的iPhone应用程序有一些问题,该应用程序具有三个计时器,其中两个是根据主计时器初始化的。假设Timer1Timer2根据主定时器的时间更改标签。现在,程序在我的预热数组中移动非常快(不是像应该的那样每30秒移动一次)。

- (void)viewDidLoad
{    
     //these arrays hold strings 
    warmup = [[NSMutableArray alloc] initWithObjects:c1,c2,c3,c4,c5,c6, nil];

    legs = [[NSMutableArray alloc] initWithObjects:l,l2,l3,l4,l5,l6,l,l2,l3,l4,l5,l6,nil];

    //main timer for the program 
    timer=[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerFired) userInfo:nil repeats:YES];
    [[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
}

-(void)timerFired
{
    if((currMinute>0 || currSeconds>=0) && currMinute>=0)
    {
        UIProgressView *progressView = [[UIProgressView alloc] init];
        progressView.frame = CGRectMake(10,350,200,500);

        [self.view addSubview:progressView];

        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]];

        [progressView setProgress:currMinute animated:YES];
    }
    else
    { 
        [timer invalidate];
    }

    if (currMinute>=27) 
    {
        title.text = @"warm-up";
        timer1 = [NSTimer scheduledTimerWithTimeInterval:30.0 target:self selector:@selector(rotatewarmup)userInfo:nil repeats:YES];
    }
    else if (currMinute<27) 
    {
        [timer1 invalidate];
        title.text = @"leg workout";
        timer2=[NSTimer scheduledTimerWithTimeInterval:60.0 target:self selector:@selector(rotatelegs)userInfo:nil repeats:YES];
        [[NSRunLoop currentRunLoop] addTimer: timer2 forMode: NSDefaultRunLoopMode];
    }
}

-(void)rotatewarmup
{
    counter++ ;
    label45.text = [NSString stringWithFormat:@"workout: %@ ", [warmup objectAtIndex:counter]];
}

//the other function does the same but with the other array. 
查尼

您的问题在于,如果currMinute> = 27,则每秒初始化一次timer1。每次初始化时,您的计数器都会增加。这就是为什么您的程序如此快速地通过热身阵列运行的原因。也许试试这个:

if (currMinute>=27) 
    {
        title.text = @"warm-up";
        if(!timer1)
            timer1 = [NSTimer scheduledTimerWithTimeInterval:30.0 target:self selector:@selector(rotatewarmup)userInfo:nil repeats:YES];
    }
else{
        [timer1 invalidate];
        title.text = @"leg workout";
        if(!timer2){            
            timer2=[NSTimer scheduledTimerWithTimeInterval:60.0 target:self selector:@selector(rotatelegs)userInfo:nil repeats:YES];
            [[NSRunLoop currentRunLoop] addTimer: timer2 forMode: NSDefaultRunLoopMode];
        }
    }

与timer2相同。工作原理:代码首先检查计时器是否已预先初始化,如果已经初始化,则不重新初始化它。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

C语言中的多个计时器

来自分类Dev

用于完成块的Objective-C计时器

来自分类Dev

Objective-C:完成间隔计时器

来自分类Dev

C#-Windows Service中的多个计时器

来自分类Dev

C#:同时执行多个计时器

来自分类Dev

使用for循环C#添加多个计时器

来自分类Dev

Unity:使用多个计时器C#

来自分类Dev

在Objective-C中另一个线程中的计时器

来自分类Dev

您如何在Objective-C中将计时器显示为屏幕上的标签?

来自分类Dev

C ++中的CPU计时器

来自分类Dev

在C ++中实现计时器

来自分类Dev

C#如何暂停计时器?

来自分类Dev

C#,WinForms,等待计时器

来自分类Dev

C ++ Ncurses显示计时器

来自分类Dev

Linux,C,如何取消计时器?

来自分类Dev

C#Blazor:倒数计时器

来自分类Dev

WPF C#-计时器倒数

来自分类Dev

计时器不停止C#

来自分类Dev

C ++ Ncurses显示计时器

来自分类Dev

C#计时器和内存

来自分类Dev

在C ++中实现计时器

来自分类Dev

Linux,C,如何取消计时器?

来自分类Dev

C#中的延迟计时器

来自分类Dev

c#倒数计时器暂停

来自分类Dev

使用System的简单示例。计时器。C#中的计时器

来自分类Dev

C ++ 11中带有计时器的计时器:从纳秒到毫秒

来自分类Dev

C#单独线程在计时器上调用方法(计时器超出范围)

来自分类Dev

C#计时器我应该使用哪个计时器?

来自分类Dev

全局计时器,即在多种形式上工作的计时器。- C#