2012-08-27 82 views
1

在我的UIViewController的loadView方法中,我啓動了一個UIActivityIndi​​cator。後來在loadView中我加載了一個複雜的UIView,大約需要2-3秒才能加載。 (更具體地說,有很多圖片的UIScrollView)。我的問題是UIActivityIndi​​cator微調僅在我的其他圖層加載後纔可見。 (這對我來說當然沒用)。什麼是處理這個問題的正確方法?顯示UIActivityIndi​​catorView直到加載UIView

- (void)loadView { 

    CGRect fullScreenRect=[[UIScreen mainScreen] bounds]; 

    UIView *contentView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 340, fullScreenRect.size.width)]autorelease]; 
    self.view = contentView; 

    spinner = [[[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite] autorelease]; 
    spinner.frame = CGRectMake(0.0, 0.0, 40.0, 40.0); 
    spinner.center = self.view.center; 
    [self.view addSubview:spinner]; 
    [spinner startAnimating]; 


    scrollView=[[[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 340, fullScreenRect.size.width)]autorelease]; 
    ...setting up scrollView... 

    [self.view addSubview:scrollView]; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    [spinner removeFromSuperview]; 
} 

我發現了一個有點類似於螺紋: UIActivityIndicatorView not showing until after loading done

但它表明加載在後臺線程的東西,但加載和顯示一個UIView只能在主線程中,據我所知。

我是初學者,抱歉,如果我的問題根本錯誤。

+1

幾個選項我看 - 你可以渲染視圖屏幕外的圖像在後臺線程並在完成後,該視圖的圖像添加到您的滾動視圖,或者你可以實現滾動視圖平鋪並按需加載滾動視圖內容。運行的儀器可能還有其他的優化。如果您需要更多幫助瞭解這些選項,請告訴我。 –

+0

爲什麼使用loadView而不使用筆尖?你是一名初學者,但正在嘗試使用先進的技術,並努力做出你需要努力的事情。所以用一個有觀點的筆尖。當你得到viewDidLoad,然後將微調器添加到你的視圖。提供關閉微調器並更新視圖的方法。在viewDidLoad中,使用默認隊列上的dispatch_async()來完成背景繁重的工作。當它完成後,讓它通過dispatch_async發送另一條消息(dispatch_get_main_queue()告訴你的viewController工作已經完成,這需要幾天的時間才能完成。 –

回答

2

我設法使它成爲Carl Veazey建議的方式。實際上這很容易。我產生了一個新的後臺線程,並在那裏加載了我的UIScrollView。

我用過,在的loadView:

spinner = [[[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite] autorelease]; 
spinner.frame = CGRectMake(0.0, 0.0, 40.0, 40.0); 
    spinner.center = self.view.center; 
[self.view addSubview:spinner]; 
[spinner startAnimating]; 

[self performSelectorInBackground:@selector(populateScrollView) withObject:nil]; 

和:

-(void) populateScrollView{ 

    ...creating scrollView... 

    [self.view addSubview:scrollView]; 
    [spinner removeFromSuperview]; 

} 

它完美。對我來說真奇怪的是,我將scrollview添加到後臺線程的UI中,而不是在主線程中。我認爲我只能在主線程中弄亂UI。

(我可以做GCD同樣的事情。當後臺線程加載完滾動視圖,我可以顯示在主線程中的隊列滾動視圖,但前一種方案莫名其妙的作品,以及...)

+0

這不是我評論的意思。內容放入圖像中,然後在主線程上使用該圖像創建圖像視圖。這個工作並不會使UIKit線程安全。它並不總是崩潰,它有時會開始表現很怪異。所以我很高興它可以工作,但是從後臺線程操縱視圖層次結構是一個編程錯誤。 –

+0

所以我想正確的方法是在後臺線程中創建我的滾動視圖,但將它添加到主線程中的視圖層次? – SPQR3

+1

觀看2012年wwdc視頻併發用戶界面。您將drawRect中繪製的內容繪製到具有繪製函數的圖像上下文中。然後將該圖像傳遞給主線程,並將其放置在圖像視圖中。它會幫助你,如果繪圖是你的瓶頸。看看視頻雖然它解釋它比我更好:) –

0

嘗試這種簡單的方法,它的工作很適合我....

UIActivityIndicatorView *activityIndicator= [[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(0, 0, 50, 50)]; 
activityIndicator.layer.cornerRadius = 05; 
activityIndicator.opaque = NO; 
activityIndicator.backgroundColor = [UIColor colorWithWhite:0.0f alpha:0.6f]; 
activityIndicator.center = self.view.center; 
activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray; 
[activityIndicator setColor:[UIColor colorWithRed:0.6 green:0.8 blue:1.0 alpha:1.0]]; 
[self.view addSubview: activityIndicator]; 

添加以下行同時加載你的觀點

//-- Add this line while processing to load your view 
    [activityIndicator startAnimating]; 

//-- Add this line when after you view loaded 
    [activityIndicator stopAnimating]; 
2

對不起,醜陋的文本格式。 有一個非常可愛的方式來做你想做的事。 首先,你必須顯示指標視圖,並且只有在稍微花費一些時間(0.1秒甚至更少)之後,纔會要求您的scrollView進行填充。

- (void)loadView { 

    CGRect fullScreenRect=[[UIScreen mainScreen] bounds]; 

    UIView *contentView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 340, fullScreenRect.size.width)]autorelease]; 
    self.view = contentView; 

    spinner = [[[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite] autorelease]; 
    spinner.frame = CGRectMake(0.0, 0.0, 40.0, 40.0); 
    spinner.center = self.view.center; 
    [self.view addSubview:spinner]; 
    [spinner startAnimating]; 

    double delayInSeconds = 0.1; 
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC)); 
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){ 
     scrollView=[[[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 340, fullScreenRect.size.width)]autorelease]; 
    ...setting up scrollView... 

     [self.view addSubview:scrollView]; 
    }); 
} 
相關問題