2014-09-30 25 views
0

我有这样的代码,总是打开一个特定的方法,持续4秒:更改的NSTimer这是循环的时间

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秒,但它似乎不工作,将使我的代码的逻辑工作的替代方法是什么?

+0

你究竟想达到...现在它会调用GO3每4秒...... – 2014-09-30 12:59:51

+0

你打电话GO3每1秒,但时间在4秒后? – 2014-09-30 13:01:25

+0

是@FahimParkar,你发布的代码适合我! – user3781174 2014-09-30 14:21:33

回答

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