更改循环中的NSTimer的时间

时间:2014-09-30 12:52:54

标签: objective-c nstimer

我有这个代码,总是打开一个特定的方法4秒钟:

float timer = 4.0;

-(void)Start{

    [NSTimer scheduledTimerWithTimeInterval:timer
                                     target:self
                                   selector:@selector(go3)
                                   userInfo:nil
                                    repeats:YES];

}

-(void)go3{

NSLog(@"Show now this messange in 1.0 seconds");

timer = 1.0;


}

这段代码的问题在于我试图将时间循环修改为4秒到1秒,但似乎没有用,有什么方法可以使我的代码逻辑工作?

2 个答案:

答案 0 :(得分:0)

您需要以下内容。

float timer = 4.0;

-(void)Start{

    [NSTimer scheduledTimerWithTimeInterval:timer
                                     target:self
                                   selector:@selector(go3)
                                   userInfo:nil
                                    repeats:NO];

}

-(void)go3{
    NSLog(@"Show now this messange in 1.0 seconds");
    timer = 1.0;
    [NSTimer scheduledTimerWithTimeInterval:timer
                                     target:self
                                   selector:@selector(go3)
                                   userInfo:nil
                                    repeats:YES];
}

答案 1 :(得分:0)

NSTimeInterval interval = 4.0f;

-(void)doSomething
{
    if (something happened) {
        interval = 1.0f; // change time when you need it
    }else if (you want to stop it) {
        return;
    }
    [self performSelector:@selector(doSomething) withObject:nil afterDelay:interval];
}