2013-01-31 62 views
4

我有一個PhotoViewController類與@property UIActivityIndicatorView* spinnerFlickrPhotoViewControllerPhotoViewController的一個子類,用於從Flickr下載照片並告訴微調者何時開始和停止動畫。 「updatePhoto」叫每個視圖控制器被賦予了Flickr照片時間:UIActivityIndi​​catorView從不動畫

- (void)updatePhoto { // Download photo and set it 
    NSLog("updatePhoto called"); 
    if (self.spinner) NSLog(@"Spinner exists in updatePhoto"); 
    dispatch_queue_t downloadQueue = dispatch_queue_create("downloader", 
                  NULL); 
    [self.spinner startAnimating]; 

    dispatch_async(downloadQueue, ^{ 
     // Download the photo 
     dispatch_async(dispatch_get_main_queue(), ^{ 
      [self.spinner stopAnimating]; 
      // Set the photo in the UI 
      } 
     }); 
    }); 
} 

上述方法正是我總是用在我的表視圖控制器顯示紡車,而表格內容下載,它在那裏工作。

你會注意到在updatePhoto的開頭,如果UIActivityIndicatorView存在,我會打印一條消息。我在awakeFromNibviewDidLoadviewWillAppear中發表了類似聲明。當我運行它,這是準確的輸出我得到:

2013-01-31 21:30:55.211 FlickrExplorer[1878:c07] updatePhoto called 
2013-01-31 21:30:55.222 FlickrExplorer[1878:c07] Spinner exists in viewDidLoad 
2013-01-31 21:30:55.223 FlickrExplorer[1878:c07] Spinner exists in viewWillAppear 

爲什麼spinnerawakeFromNib存在嗎? Docs指出:「當一個對象收到一個awakeFromNib消息時,它確保所有的出口和動作連接已經建立。」 IBOutlet可以在沒有連接到的對象的情況下連接嗎?在這種情況下,可以將spinner IBOutlet連接到故事板而不分配spinner

超出此範圍,我將覆蓋spinner的獲取方,以便它在不存在的情況下實例化。因此,打印輸出現在看起來像這樣:

2013-01-31 21:48:45.646 FlickrExplorer[2222:c07] Spinner exists in awakeFromNib 
2013-01-31 21:48:45.647 FlickrExplorer[2222:c07] updatePhoto called 
2013-01-31 21:48:45.647 FlickrExplorer[2222:c07] Spinner exists in updatePhoto 
2013-01-31 21:48:45.649 FlickrExplorer[2222:c07] Spinner exists in viewDidLoad 
2013-01-31 21:48:45.650 FlickrExplorer[2222:c07] Spinner exists in viewWillAppear 

這是我本來預期會看到的。不過,我仍然沒有得到任何動畫。

,我已經排除了可能出現的問題:

所有這三種可能性的事實,把[self.spinner startAnimating];viewWillAppear使得整個下載過程順利動畫排除。如果你喜歡,你可以下載this project。只需轉到任何嘗試顯示大圖片的屏幕,您都會看到微調不出現。這個項目有很多問題,但這是我現在關注的問題。

編輯1:

  • 我添加上的Git項目的缺少相關性,因此該項目將 現在編譯您

編輯2(2013年2月2日):

  • 由於另一個視圖控制器的prepareForSegue設置了updatePhoto方法,因此我只在iPhone上看到此問題照片在FlickrPhotoViewController。這是否有可能導致這個問題?
+3

爲什麼不只是使用MBProgressHUD https://github.com/jdg/MBProgressHUD並讓自己擺脫所有那些'ActivityIndi​​cator'的煩惱 –

+0

我已經閱讀了這麼好的簡潔問題已經很長時間了的研究努力。 +1。接受我的建議:不要使用界面「蹩腳的」Builder來強調自己。代碼工作得很好。 – 2013-01-31 17:26:22

+0

+1對問題的完整性。激勵我獲取您的代碼並瀏覽它。難以測試,因爲它遠不及編譯。我注意到了一些事情:你的視圖在IB中是白色的,而且你在問題中引用的代碼被setPhoto:setter調用,它由外部VC調用,它完全不清楚你的FlickrPhotoViewController是否正確在這個UI相關的東西開始發生之前初始化。 – danh

回答

1

嘗試火啓動動畫uiactivityindictorü調用updatePoto方法這樣 神祕召喚調用UpdatePhoto這iinstead只是衆人面前:

[self startAnimatingAndThenUpdatePhoto]; 


-(void)startAnimatingAndThenUpdatePhoto{ 
if (self.spinner) NSLog(@"Spinner exists in updatePhoto"); 
    [self.spinner startAnimating]; 
[self performSelector:@selector(updatePhoto) withObject:nil afterDelay:0.01]; 
} 
- (void)updatePhoto { // Download photo and set it 
     NSLog("updatePhoto called"); 
     dispatch_queue_t downloadQueue = dispatch_queue_create("downloader", 
                   NULL); 

     dispatch_async(downloadQueue, ^{ 
      // Download the photo 
      dispatch_async(dispatch_get_main_queue(), ^{ 
       [self.spinner stopAnimating]; 
       // Set the photo in the UI 
       } 
      }); 
     }); 
    } 

不是世界上最好的方法,但它會做thejob

+0

呃!這工作!但爲什麼!?早些時候,我發現一個StackOverflow的答案提示周圍做它的其他方式: ' - (無效){updatePhoto [自performSelector:@selector(startSpinning)withObject:無afterDelay:0.01]; //代碼更新照片 ' 但是,這並不適用於我。通過'performSelector'調用哪個方法是可行的!但是,這是一個可怕的,邪惡的,醜陋的解決方案!爲什麼這裏需要這個,而不是其他地方?爲什麼我的代碼中有一個硬編碼的0.01!? –

+0

我犯了一個大錯。你完全可以忽略我的最後一條評論。恐怕這個想法根本不能解決問題。我在應用此修復程序後在iPad上測試了它。但是,我從來沒有在iPad上使用微調器動畫的問題。像以前一樣,它在iPhone上仍然無法使用。 –

相關問題