如何延迟进行瞄准 [英] How to Perform Segue With Delay

查看:103
本文介绍了如何延迟进行瞄准的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的故事板中,我有一个视图作为启动画面。在这个视图中,我已经有一个像打开应用程序的按钮,它打开菜单视图,带有模态segue。但我也希望屏幕自动执行segue,就像2秒钟后出现一样。

In my storyboard, I have a view as a splash screen. In this view, I already have a button like "Open Application" that is opening the menu view with a modal segue. But I also want screen to perform segue automatically, like after 2 seconds view appears.

这里的一些代码:

- (void)viewDidAppear:(BOOL)animated
{
    [self performSegueWithIdentifier:@"splashScreenSegue" sender:self];
}

如您所见,我已经使用了performSegueWithIdentifier,但它立即执行。有没有办法让它延迟?

As you can see, I already use performSegueWithIdentifier but it performs immediately. Is there a method to make it delay?

提前致谢。

推荐答案

你可以使用GCD的 dispatch_after 在视图出现后2秒执行你的segue代码,例如:

You can use GCD's dispatch_after to execute your segue code 2 seconds after the view appears, e.x:

- (void)viewDidAppear:(BOOL)animated 
{
    [super viewDidAppear:animated];

    double delayInSeconds = 2.0;
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        [self performSegueWithIdentifier:@"splashScreenSegue" sender:self];
    });
}

此外,请确保在重写UIViewController时记得调用super实现生命周期方法。

Additionally, please make sure that you remember to call the super implementation when overriding UIViewController's life cycle methods.

这篇关于如何延迟进行瞄准的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆