2012-06-06 54 views
1

我想實現一個有多個UIViews的滾動視圖。最左邊的項目需要大於其餘項目。所以我的問題是這樣的。每當一個項目離開屏幕到左邊(其origin.x小於15),我需要將項目從470x440縮小到235x220像素。這很容易實現。問題在於,移動到像素480左側的項目需要從235x220像素縮放到470x440像素,並且需要將其移動到左側235像素(以便不覆蓋其右側的項目,而是移動到離開元素「縮水」時留下的空間UIScrollView與子視圖縮放

我已經嘗試了幾種不同的方法來解決這個問題,但是我不能將動畫看起來很好,並且這裏有一堆毛刺,

有沒有人有任何想法我可能會去實現這種類型的功能?請注意,我不想縮放,但我想要調整滾動視圖中的元素的方式,大多數元素(在屏幕上可見)是其他元素的兩倍。

+1

結帳[高級滾動技巧 - WWDC2011](https://developer.apple.com/videos/wwdc/2011/?id=104)。你應該在視頻結尾處找到你的答案 - 但是從一開始就需要注意(你需要使用開發者ID登錄)。 –

+0

感謝您的鏈接rokjarc!它確實討論了縮放,但我並沒有放大整個滾動視圖的內容,而是調整了滾動視圖中的一些元素的大小。但是這個視頻給了我一些好的啓示:) –

回答

3

萬一別人可能會感興趣,我結束了內部scrollViewDidScroll如下:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {   
    float contentOffset = scrollView.contentOffset.x; 
    int leavingElementIndex = [_scrollView indexOfElementLeavingScene:scrollView.contentOffset.x]; 
    int entereingElementIndex = leavingElementIndex + 1; 

    if (leavingElementIndex >= 0 && contentOffset > 0) { 
     CGRect leavingFrame = [[[scrollView subviews] objectAtIndex:leavingElementIndex] frame]; 
     CGRect enteringFrame = [[[scrollView subviews] objectAtIndex:entereingElementIndex] frame]; 

     float scalePerentage = (contentOffset - (_scrollView.smallBoxWidth * leavingElementIndex))/(_scrollView.smallBoxWidth); 
     enteringFrame.size.width = _scrollView.smallBoxWidth + (_scrollView.smallBoxWidth * scalePerentage); 
     enteringFrame.size.height = _scrollView.smallBoxHeight + (_scrollView.smallBoxHeight * scalePerentage); 
     enteringFrame.origin.x = [_scrollView leftMostPointAt:entereingElementIndex] - (_scrollView.smallBoxWidth * scalePerentage); 

     [[[scrollView subviews] objectAtIndex:entereingElementIndex] setFrame:enteringFrame]; 

     leavingFrame.size.width = _scrollView.largeBoxWidth - (_scrollView.smallBoxWidth * scalePerentage); 
     leavingFrame.size.height = _scrollView.largeBoxHeight - (_scrollView.smallBoxHeight * scalePerentage); 

     [[[scrollView subviews] objectAtIndex:leavingElementIndex] setFrame:leavingFrame]; 

     //Reset the other visible frames sizes 
     int index = 0; 
     for (UIView *view in [scrollView subviews]) { 

      if([view isKindOfClass:[SlidingView class]] && index > entereingElementIndex) { 
       CGRect frame = view.frame; 
       frame.size.width = _scrollView.smallBoxWidth; 
       frame.size.height = _scrollView.smallBoxHeight; 
       frame.origin.x = [_scrollView leftMostPointAt:index]; 
       [view setFrame:frame]; 
      } 

      index++; 
     } 

    }  
} 

這是什麼樣子到底:

End Result http://stuff.haagen.name/iOS%20scroll%20resize.gif

+0

這在以下項目中得到了更充分的實現:https://github.com/kantega/meetingrooms –