2011-09-28 41 views
2

這可能是一個奇怪的問題,但可能有不同的解決方案,我試圖實現,我願意爲任何需要的東西開放!讓線程進入睡眠狀態,但保持動畫 - 這可能嗎?

事實:

我有一個UISearchDisplayController處理的UISearchBar一個UITableView

上面說的TableView我有一些按鈕和一個小UIScrollView

當用戶點擊SearchBar並彈出鍵盤時,輸入時顯示搜索結果的空間很小。

因此,我想在用戶開始搜索時將TableView一直移動到頂部(覆蓋按鈕和ScrollView)。

我的代碼和問題:

- (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller { 
... 

[UIView beginAnimations:@"Search" context:nil]; 
[UIView setAnimationDuration:0.6]; 
[self.attractionsTableView setFrame:CGRectMake(0, 0, 320, 400)]; 
[UIView commitAnimations]; 

... 
return; 

}

現在的問題是,既然我UITableView要旅行的距離大約是100分,SearchDisplayController是把黑色覆蓋上更快TableView的頂部,因此黑色覆蓋層已經在頂部,而TableView仍然在那裏滑動。

結果是一個屏幕,看起來有點像這樣:

problem http://img833.imageshack.us/img833/9186/problemqdu.png

所以用戶看到的TableView向上滑動(這將是罰款)​​,但黑色覆蓋已經在那裏等候,因爲searchDisplayController的動畫比我的快。

所以我有一個可能的想法,但不知道如何做到這一點:

有沒有一種方法來設置searchDisplayController把黑色覆蓋上搜索的tableView的頂部動畫的持續時間?

編輯:

我知道我可以animationDuration設定爲0.01,甚至爲0,但0.6的速度似乎對於100點的距離非常好,所以我試圖找到降低不同的解決辦法持續時間。

回答

3

訣竅是不讓運行循環正常運行,直到動畫完成。當運行循環以特定模式運行時,會執行動畫。幸運的是,疊加動畫是以不同的運行循環模式執行的,而不是其他運行循環模式下運行的動畫。所以,你所要做的就是從你的方法中運行運行循環,直到動畫完成。

- (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller { 
    BOOL done = NO; 
    // Pass the done variable's address as the context so we can be notified 
    [UIView beginAnimations:@"SearchExpand" context:&done]; 
    [UIView setAnimationDelegate:self]; 
    [UIView setAnimationDidStopSelector:@selector(animationDone:finished:context:)]; 
    [UIView setAnimationDuration:0.6]; 
    // Expend the table view to fill its superview 
    self.attractionsTableView.frame = [[self.attractionsTableView superview] bounds]; 
    [UIView commitAnimations]; 
    NSRunLoop *runLoop = [NSRunLoop currentRunLoop]; 
    // Run the run loop until the animation completes 
    while(!done) { 
     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 
     if(![runLoop runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]]) 
      break; 
     [pool drain]; 
    } 
} 

- (void)animationDone:(NSString *)name finished:(NSNumber *)finished context:(void *)context { 
    // Set the done flag to YES 
    *((BOOL*)context) = YES; 
    // and kill the run loop 
    CFRunLoopStop(CFRunLoopGetCurrent()); 
} 
+0

工程就像一個魅力!驚人! :)你是男人! 雖然有一個問題:(void)animationDone方法中的(NSNumber *)完成的參數是什麼? – Octoshape

+0

另一個問題:有沒有關於這方面的任何文檔?因爲我有點理解它,但我不認爲我可以在任何其他環境中使用它。有沒有辦法找出在這個「不同的運行循環模式」下是否執行動畫? – Octoshape

+0

第三個問題(對不起)..我不明白爲什麼我們在while循環中創建一個新的NSAutoreleasePool。我只是試着評論它,可能會發現一個例外,它可以幫助我理解它的用途,但它仍然可以在沒有游泳池的情況下運行。這是不好的代碼還是我創建了內存泄漏或什麼? – Octoshape

相關問題