简体   繁体   English

滚动时调整UICollectionView的大小

[英]Resize UICollectionView while scrolling

My collection view begins life short, at the bottom of it's view controller's view. 我的集合视图开始时间很短,位于视图控制器视图的底部。 When the user scrolls up a little bit, I'd like the collection to fill the view. 当用户向上滚动一点时,我希望该集合能够填充视图。 I'd like it to shrink again when the user pulls down into the bounce area. 当用户拉下弹跳区域时,我希望它再次缩小。

Here's the code that I'd like to make work (and I think should work): 这是我想要工作的代码(我认为应该工作):

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (scrollView == self.collectionView) {
        CGFloat offsetY = self.collectionView.contentOffset.y;
        if (offsetY > 20.0) {
            [self setCollectionViewExpanded:YES];
        } else if (offsetY < -10.0) {
            [self setCollectionViewExpanded:NO];
        }
    }
}

- (void)setCollectionViewExpanded:(BOOL)expanded {

    // avoid starting a do-nothing animation
    BOOL currentlyExpanded = self.collectionView.frame.origin.y == 0.0;
    if (expanded == currentlyExpanded) return;

    // note: the collection view is the uppermost subview of the vc's view
    // expanding to view bounds makes it cover every other view
    CGRect newFrame = (expanded)? self.view.bounds : CGRectMake(0.0, 240.0, 320.0, self.view.bounds.size.height-240.0);
    [UIView animateWithDuration:0.3 animations:^{
        self.collectionView.frame = newFrame;
    }];
}

But after the expand happens, the cells remain in the same position relative to the parent view (still near the bottom) and the collection view stops responding to panning touches and sends no further didScroll notification. 但是在扩展发生后,单元格保持在相对于父视图的相同位置(仍然靠近底部),并且集合视图停止响应平移触摸并且不再发送didScroll通知。

I can get the content repositioned properly by doing a reloadData in an animation completion block, but the scrolling touches stop working either way. 我可以通过在动画完成块中执行reloadData来正确地重新定位内容,但滚动触摸会以任何方式停止工作。

A while back, I tried something like this with a table view (or another kind of scroll view) and ran into a similar hangup. 不久前,我尝试了这样的表格视图(或其他类型的滚动视图)并遇到了类似的挂断。 Much obliged if anyone has this solved... 如果有人解决了这个问题,那就太大了......

Can you describe your view hierarchy a bit better? 你能描述一下你的视图层次结构好一点吗? Is the UICollectionView a subview of the UIScrollView? UICollectionView是UIScrollView的子视图吗?

Is self.view a UIScrollView? self.view是UIScrollView吗?

  • In that case, the bounds origin is the scrollview's contentOffset (not 0,0). 在这种情况下,bounds origin是scrollview的contentOffset(不是0,0)。 That could give your UICollectionView an unexpected frame. 这可能会给你的UICollectionView一个意想不到的框架。
  • If the UICollectionView is a subview of the scrollview, the frame origin is the origin of the view in the scrollview-space. 如果UICollectionView是scrollview的子视图,则框架原点是scrollview-space中视图的原点。 So don't change the frame's origin (just its size) and set the contentOffset of the scrollView to make it visible. 因此,不要更改框架的原点(只是它的大小)并设置scrollView的contentOffset以使其可见。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM