暂停视图更新

发布于 2025-01-08 18:13:07 字数 1415 浏览 0 评论 0原文

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

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

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

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

Want to pause the multiple views from updating when Pause button is pressed

In h file

@property BOOL appIsPaused;

In m file

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

and it goes on like this for multiple views.

Any ideas how to pause views from updating whenever pause button is pressed.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

幽梦紫曦~ 2025-01-15 18:13:07

您应该考虑使用 NSTimer,而不是在延迟后使用 PerformSelector。

像这样:

声明一个 NSTimer *timer 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]

它也更干净,让您可以更好地控制代码。

希望有帮助。

Instead of using performSelector after delay, you should consider using a NSTimer.

Like this:

Declare a NSTimer *timer ivar.
Declare a NSUInteger viewControl;

Set viewControl to 0;

On the play part of the method add this line:

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

And add this line on pause action:

[timer invalidate]

It is also cleaner and let you have more control over your code.

Hope it helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文