gpt4 book ai didi

iphone - 滚动时调整 UICollectionView 的大小

转载 作者:可可西里 更新时间:2023-11-01 06:16:01 24 4
gpt4 key购买 nike

我的 Collection View 开始时很短,在它的 View Controller View 的底部。当用户向上滚动一点时,我希望集合能够填满 View 。当用户向下拉进入弹跳区域时,我希望它再次收缩。

这是我想要使用的代码(我认为应该可以使用):

- (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;
}];
}

但是在展开发生后,单元格相对于父 View 保持相同的位置(仍然靠近底部)并且 Collection View 停止响应平移触摸并且不再发送 didScroll 通知。

我可以通过在动画完成 block 中执行 reloadData 来正确地重新定位内容,但无论哪种方式,滚动触摸都会停止工作。

前阵子,我尝试使用表格 View (或另一种 ScrollView )进行类似的操作,但遇到了类似的挂起问题。如果有人解决了这个问题,我将不胜感激......

最佳答案

你能更好地描述一下你的 View 层次结构吗? UICollectionView 是 UIScrollView 的 subview 吗?

self.view 是 UIScrollView 吗?

  • 在这种情况下,边界原点是 ScrollView 的 contentOffset(不是 0,0)。这可能会给您的 UICollectionView 一个意想不到的框架。
  • 如果 UICollectionView 是 ScrollView 的 subview ,则框架原点是 ScrollView 空间中 View 的原点。所以不要更改框架的原点(只是它的大小)并设置 scrollView 的 contentOffset 以使其可见。

关于iphone - 滚动时调整 UICollectionView 的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15492049/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com