Ios5 如何在实例化耗时的ui元素期间更新progressbar?

Ios5 如何在实例化耗时的ui元素期间更新progressbar?,ios5,uikit,Ios5,Uikit,我想在实例化一些需要花费一些时间的ui元素时更新progressbar。我首先在viewLoad方法中创建我的视图,并在那里添加我的进度条。一旦我的视图出现在ViewDidAspect方法中,我将实例化几个uikit对象,但同时我想更新进度条。我不知道如何继续,因为一切都应该发生在主线程中,因为它是ui元素 以下是我的部分代码: -(void) viewDidAppear:(BOOL)animated { // precompute the source and destination

我想在实例化一些需要花费一些时间的ui元素时更新progressbar。我首先在viewLoad方法中创建我的视图,并在那里添加我的进度条。一旦我的视图出现在ViewDidAspect方法中,我将实例化几个uikit对象,但同时我想更新进度条。我不知道如何继续,因为一切都应该发生在主线程中,因为它是ui元素

以下是我的部分代码:

-(void) viewDidAppear:(BOOL)animated
{
    // precompute the source and destination view screenshots for the custom segue
    self.sourceScreenshotView = [[UIImageView alloc] initWithImage:[self.view pw_imageSnapshot]];

    [self.progressBar setProgress:.3];


    SCLViewController *rvc = [[SCLViewController alloc] init];
    UIView *destinationView = rvc.view;
    destinationView.frame = CGRectMake(0, 0, kWidthLandscape, kHeightLandscape);


    self.destinationScreenshotView = [[UIImageView alloc] initWithImage:[destinationView pw_imageSnapshot]];

    [self.progressBar setProgress:.5];

}
在上面的代码中,我只需要创建两个视图截图,以便以后使用。问题是,我只看到最后一次更新。5当设置进度条的进度时。进行此更新的正确方法是什么

您可以使用该方法来实例化重视图。实例化视图的方法必须在主线程中设置进度条进度

因此,您的代码如下所示:

- (void)viewDidAppear:(BOOL)animated
{
    [self performSelectorInBackground:@selector(instantiateHeavyViews) withObject:nil];
}

- (void)instantiateHeavyViews
{
    self.sourceScreenshotView = [[UIImageView alloc] initWithImage:[self.view pw_imageSnapshot]];
    [self performSelectorOnMainThread:@selector(updateMyProgressView:) withObject:[NSNumber numberWithFloat:0.3f] waitUntilDone:YES];

    SCLViewController *rvc = [[SCLViewController alloc] init];
    UIView *destinationView = rvc.view;
    destinationView.frame = CGRectMake(0, 0, kWidthLandscape, kHeightLandscape);

    self.destinationScreenshotView = [[UIImageView alloc] initWithImage:[destinationView pw_imageSnapshot]];

    [self performSelectorOnMainThread:@selector(updateMyProgressView:) withObject:[NSNumber numberWithFloat:0.5f] waitUntilDone:YES];
}

- (void)updateMyProgressView:(NSNumber *)progress
{
    [self.progressBar setProgress:[progress floatValue]];
}

编辑:当然,它不会为你的进度条设置动画,我不知道这是否是你想要的。如果希望在创建视图时继续进行,则应该使用代理来通知进度,这可能会有点困难。这样,您就可以在每次通知学员时更新进度条。

它可以工作-谢谢!那么在后台线程上执行UIVIew实例化就可以了?我认为uikit对象的所有操作都应该只在主线程上完成。您还可以详细说明一下如何平滑地更新进度,直到每次实例化后达到我设置的进度吗?是的,您可以在后台线程中实例化视图。在主线程上,您需要做的是暗示重画的所有其他事情,如添加子视图或修改某些属性。我告诉你的关于顺利进展的事情,你应该有一些设计,让你知道你每时每刻都处于什么状态,以便更新进度条。现在,您只知道初始化何时开始,何时结束。如果此初始化是一系列已知步骤,则可以在这些步骤之间添加一些代码,以告知委托人更新进度条。