1

我有一个 UIProgressView 我想在我的 GLKViewController 上运行,而其余的代码正在由 iOS 加载。我已将 UIProgressView 放入我的“viewDidLoad”方法中。在 viewDidLoad 方法加载所有代码之前,UIProgressView 不会显示。如何在调用 viewDidLoad 方法后立即显示 UIProgressView 并在 viewDidLoad 方法完成时结束?

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Progress Bar
    [threadProgressView setProgress: 0.0];    
    [self performSelectorOnMainThread:@selector(updateProgressBar) withObject:nil waitUntilDone:NO];

    // Disbale iPhone from locking
    [[UIApplication sharedApplication] setIdleTimerDisabled: YES];

    // Set up context to use Open GL ES
    self.context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];

    if (!self.context) {
        NSLog(@"Failed to create ES context");
    }

    // Create a view to display the Open GL ES content
    GLKView *view = (GLKView *)self.view;
    view.context = self.context;
    view.drawableDepthFormat = GLKViewDrawableDepthFormat24;

    /******** Default Settings **********/
    m_kf            = 0;                                    // The current keyframe ID.
    m_avPos         = GLKVector3Make(0.0f, 0.0f, -35.0f);   // Where to put the avata.
    m_camPos        = GLKVector3Make(0.0f, 10.0f, -35.0f);  // The camera orbits this point.
    m_camDist       = 30.0f;                                // Distance of camera from the orbit point.

    /******** Set up open gl ***********/
    [self setupGL];

    /********* TOUCH IMPLEMENTATION *********/

    // Pinch recongizer detects pinches and is used to zoom in/out
    UIPinchGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchDetected:)];
    [self.view addGestureRecognizer:pinchRecognizer];

    // Pan recognizer, used to rotate around the x and y axis
    UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panDetected:)];
    [self.view addGestureRecognizer:panRecognizer];

    /********* END TOUCH IMPLEMENTATION *********/

}
4

3 回答 3

1

如何在调用 viewDidLoad 方法后立即显示 UIProgressView 并在 viewDidLoad 方法完成时结束?

viewDidLoad被调用时,视图还没有出现在屏幕上。之后viewDidLoad你会看到viewWillAppear然后viewDidAppear被调用。

而且,如果您长时间运行viewDidLoad,它将阻塞您的主线程,因此您的进度条将不会更新。
当你这样做:

 [self performSelectorOnMainThread:@selector(updateProgressBar) withObject:nil waitUntilDone:NO];    

由于您在主线程上,因此指定waitUntilDone会将您的请求排队并稍后处理,可能在您viewDidLoad完成之后。

为了做你想做的事,你需要更多的异步代码。

于 2013-04-01T13:32:14.157 回答
0

viewDidLoad 在主循环,主线程上执行。那是用户界面线程。结束后界面上的任何变化都不会显示出来。所以,这是正常的行为。

选择器 updateProgressBar 将在 viewDidLoad 在同一线程(主线程)上结束后执行。

仅使用 viewDidLoad 进行初始化,然后在后台线程上执行进程并在主线程(即接口线程)上执行 progressView 更改。

于 2013-04-01T13:32:39.307 回答
0

在主线程(这是您的默认设置)上添加:

[[NSRunLoop currentRunLoop] runUntilDate: [NSDate distantPast]];

这将给主循环一个单一的镜头,然后返回到您的代码。

于 2018-12-13T13:57:16.507 回答