2012-06-06 138 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 –

相关问题