2012-03-29 92 views
8

當我在UITableView中選擇一行時,我在行框架的GCRect上調用scrollRectToVisible:animated,然後立即執行一些其他動畫。我的問題是,我不知道scrollRectToVisible:animated的動畫何時完成。在UITableView中,我如何知道scrollRectToVisible何時完成一行?

我的代碼:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    UITableViewCell *cell = [tableView cellForRwoAtIndexPath:indexPath]; 

    [self.tableView scrollRectToVisible:cell.frame animated:YES]; 

    //more animations here, which I'd like to start only after the previous line is finished! 
} 
+0

+1好問題,但恐怕答案是:你不知道什麼時候scrollRectToVisible:animated:'完成。 – Sam 2012-03-29 16:16:52

+0

下面的問題的答案也可以在這裏幫助:http://stackoverflow.com/questions/7198633/how-can-i-tell-when-a-uitableview-animation-has-finished – fishinear 2013-07-29 16:35:28

回答

3

協議UITableViewDelegate符合UIScrollViewDelegate。您可以設置BOOL參數,當你手動滾動比簽入scrollViewDidScroll:

BOOL manualScroll; 
... 
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    UITableViewCell *cell = [tableView cellForRwoAtIndexPath:indexPath]; 

    manualScroll = YES; 
    [self.tableView scrollRectToVisible:cell.frame animated:YES]; 
} 
... 
- (void)scrollViewDidScroll:(UIScrollView *)scrollView 
{ 
    if (manualScroll) 
    { 
     manualScroll = NO; 
     //Do your staff 
    } 

} 

不要忘記設置UITableViewDelegate

+0

這是相當黑客,但看起來它會起作用,並且在給定框架的情況下你可以做的最好。感謝你的回答! – 2012-04-24 02:25:08

+0

UIScrollView上有'dragging'屬性,爲你做手動滾動跟蹤 – 2014-12-13 18:55:37

15

我跑過這個UIScrollViewDelegate方法:

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView 
{ 
    // Do your stuff. 
} 

只有呼籲動畫滾動。不需要基於觸摸的卷軸。似乎很好。

+0

正是我在找的,謝謝! – 2013-09-18 01:02:46

5

更簡單的方法是封裝的滾動代碼在一個UIView animateWith [...]塊,像這樣:

[UIView animateWithDuration:0.3f animations:^{ 
    [self.tableView scrollRectToVisible:cell.frame animated:NO]; 
} completion:^(BOOL finished) { 
    // Some completion code 
}]; 

注意,動畫==否下,scrollRectToVisible:動畫:方法。

+0

這應該是anwser – 2016-08-03 12:30:10

相關問題