重新启动计时器

时间:2012-02-25 19:53:51

标签: iphone ios5 xcode4.2

暂停视图更新时间无效,因为点击暂停按钮暂停音频文件。

-(void)playpauseAction:(id)sender 
{

if  

  ([audioPlayer isPlaying]){

 [sender setImage:[UIImage imageNamed:@"play.png"] forState:UIControlStateSelected];

  [audioPlayer pause];

 [timer invalidate];

  } else {

 [sender setImage:[UIImage imageNamed:@"pause.png"] forState:UIControlStateNormal];

  [audioPlayer play];

  self.timer = [NSTimer scheduledTimerWithTimeInterval:11 target:self selector:@selector(displayviewsAction:) userInfo:nil repeats:NO];

  }  

}

- (void)displayviewsAction:(id)sender
{
FirstViewController *viewController = [[FirstViewController alloc] init];

viewController.view.frame = CGRectMake(0, 0, 320, 480);

[self.view addSubview:viewController.view];

[self.view addSubview:toolbar];

self.timer = [NSTimer scheduledTimerWithTimeInterval:23 target:self selector:@selector(secondViewController) userInfo:nil repeats:NO];

[viewController release];

}

-(void)secondViewController {
SecondViewController *secondController = [[SecondViewController alloc] init];

secondController.view.frame = CGRectMake(0, 0, 320, 480);

[self.view addSubview:secondController.view]; 

[self.view addSubview:toolbar];

self.timer = [NSTimer scheduledTimerWithTimeInterval:27 target:self selector:@selector(ThirdviewController) userInfo:nil repeats:NO];

[secondController release];
}  

对于多个视图,它是这样的。根据应用程序逻辑,用户可以在任何视图上暂停以暂停音频文件,而计时器invalidate语句也会暂停视图的更新。当用户再次点击暂停以恢复音频文件时,它应该从该点开始更新视图。所以我遇到的问题是如何重新启动计时器,以便无论用户暂停哪个视图,它都会继续更新视图。

无论我怎么做。

2 个答案:

答案 0 :(得分:1)

定时器不应该重复使用,只需创建另一个定时器。

答案 1 :(得分:1)

-(void)countDown{
    counter--;
    if (counter == 0){
        [self.timer invalidate];
        [self secondViewController];
    }
}

启动计时器:

self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(countDown) userInfo:nil repeats:YES];

重新启动你的计时器:

counter = 23;
[self.timer fire];

你应该为你的目的调整这个答案,因为你可以看到,计时器只安排用于启动-(void)secondViewController方法