暂停更新视图

时间:2012-02-24 18:07:33

标签: iphone ios5 xcode4.2

想要在按下暂停按钮时暂停多个视图的更新

在h档案中

@property BOOL appIsPaused;

在m档案中

@synthesize appIsPaused;

-(void)playpauseAction:(id)sender 
{

if  

  ([audioPlayer isPlaying]){

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

      [audioPlayer pause];

      appIsPaused = YES;

  } else {

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

      [audioPlayer play];

      appIsPaused = NO;

      [self performSelector:@selector(displayviewsAction:) withObject:nil afterDelay:11.0];

  }  

}

- (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 performSelector:@selector(secondViewController) withObject:nil afterDelay:23];

[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 performSelector:@selector(ThirdviewController) withObject:nil afterDelay:27];

[secondController release];
}

对于多个视图,它会像这样继续。

任何想法如何在按下暂停按钮时暂停更新视图。

1 个答案:

答案 0 :(得分:1)

不要在延迟后使用performSelector,而应考虑使用NSTimer。

像这样:

声明NSTimer *计时器ivar。 声明NSUInteger viewControl;

将viewControl设置为0;

在方法的播放部分添加以下行:

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

-(void)tick
{
    switch(viewControl)
    {
        case 0:
            [self performSelector:@selector(firstViewController) withObject:nil];
            break;
        case 1:
           [self performSelector:@selector(secondViewController) withObject:nil];
            break;
        case 2:
           [self performSelector:@selector(thirdViewController) withObject:nil];
           break;
        .
        .
        .
        default:
            break;
    }

    viewControl++;

    if(viewControl > MAX_VIEWS)
    {
         viewControl = 0;
    }
}

在暂停操作中添加以下行:

[timer invalidate]

它也更清晰,让您可以更好地控制代码。

希望它有所帮助。