2014-11-01 108 views
0

我在viewController.view中添加了約500個視圖。 這個動作花費了大約5秒的時間。 現在,我希望在添加每個子視圖後刷新屏幕,以便用戶在屏幕上看到它們逐個顯示。setNeedsDisplay添加子視圖後無法正常工作

我在的viewController嘗試這樣:

-(void)viewDidAppear:(BOOL)animated 
{ 
    [super viewDidAppear:animated]; 
    for(int i=0; i<500; i++) 
    { 
     //...Create aView 
     [self.view addsubview:aView]; 
     [self.view setNeedsDisplay]; 
    } 
} 

我運行它,什麼都沒有發生,持續5秒,那麼所有的意見出現了一次。 我確保[self.view setNeedsDisplay]從主線程上下文中調用。

任何想法如何使這些子視圖逐一出現?

+0

你或許應該閱讀'setNeedsDisplay'的文檔。它不會強制重繪,也不應該重繪。 – 2014-11-01 11:23:18

+1

根據你想要做的視覺效果,定時器,運行循環和動畫是研究的其他好主題。 – 2014-11-01 11:25:02

回答

0

我找到了一個簡單的解決方案。我添加了一個新的屬性,以我的viewController - 「subviewsCount」女巫被初始化爲500,然後叫從viewDidLoad中下面的方法:

-(void) addSubviewsToMotherView 
{ 
    self.subviewsCount -=1; 
    if (self.subviewsCount >= 0) 
    { 
     [UIView animateWithDuration:0.0 
           delay:0.0 
          options:UIViewAnimationOptionLayoutSubviews 
         animations:^{ 
          [self methodToAddSubview]; 
         } 
         completion:^(BOOL finished){ 
          [self addSubviewsToMotherView]; 
         } 
     ]; 

    } 
} 
相關問題