2012-07-17 98 views
0

例如在Tweetbot的iPhone應用程序中。當你打開應用程序併發布新的推文時,它只會附加到UIScrollView的頂部,而你看到的當前推文並未刷新。我怎樣才能達到同樣的效果?說我有一個UIScrollView UIViewView添加到UIScrollView從原點10,10開始。將UIView添加到UIScrollView的頂部

然後我下載了一些項目,我想把它放在10,10 ..所以我基本上需要將這個舊項目在10,10右移?如果我這樣做的話,那麼在這個轉換過程中,用戶會看到它轉移,這不是我想要的效果。在Tweetbot的應用程序中,似乎沒有任何東西正在轉移,只是你在10,10以上的區域增加了區域並在那裏添加了新的東西。

我該怎麼做?

基本上我想在UIScrollView中實現insertRowAtIndexPath。

回答

0

將以這種方式重申問題:如何在不移動已存在的內容(相對於當前偏移量)的情況下將內容添加到UIScrollView的頂部。

如果這是正確的問題,正確的答案是按照您的建議進行添加和向下移動,但隨後滾動與添加內容相同的高度,從而導致舊內容無法移動的錯覺。

- (void)insertRowAtTop { 

    // not showing insert atIndexPath because I don't know how you have your subviews indexed 
    // inserting at the top, we can just shift everything down. you can extend this idea to 
    // the middle but it requires that you can translate from rows to y-offsets to views 

    // shift everything down 
    for (UIView *view in self.scrollView.subviews) { 
     if ([view isKindOfClass:[MyScrollViewRow self]]) { 
      MyScrollViewRow *row = (MyScrollViewRow *)view;   // all this jazz so we don't pick up the scroll thumbs 
      row.frame = CGRectOffset(row.frame, 0.0, kROW_HEIGHT); // this is a lot easier if row height is constant 
     } 
    } 
    MyScrollViewRow *newRow = [[MyScrollViewRow alloc] initWithFrame:CGRectMake(0.0,0.0,320.0,kROW_HEIGHT)]; 
    // init newRow 
    [self.scrollView addSubview:newRow]; 

    // now for your question. scroll everything so the user perceives that the existing rows remained still 
    CGPoint currentOffset = self.scrollView.contentOffset 
    self.scrollView.contentOffset = CGPointMake(currentOffset.x, currentOffset.y + kROW_HEIGHT); 
} 
0

如果您設置contentOffset而沒有動畫,那麼不會是可見的滾動動畫。因此,如果您的新視圖高200點,您可以將新視圖的原點設置爲(10,10),將舊視圖設置爲(10,210),並將滾動視圖的contentOffset設置爲(10,210),則應該達到您打算。您還需要增加滾動視圖的contentSize,使其足以容納它包含的所有內容。