6

我正在尝试实现具有无限滚动行为的集合视图。滚动应该是圆形的,就像UIPickerView. 可以用 Apple 完成UICollectionView还是别无选择,只能创建一个水平 PickerView ?

你怎么看?

4

4 回答 4

3

您应该观看 WWDC 2011 会议视频“高级 ScrollView 技术”,他们谈论无限滚动,我很确定可以将它与UICollectionViews 一起使用。它在您滚动时居中,并将新项目放置在当前可见项目之后(或之前,取决于滚动方向)。

就像他们在会议中所说的那样,您不应该使用有结局的方法。正如会议中所说:人们肯定会在某一时刻找到优势。

于 2013-03-22T09:16:04.397 回答
-1

是的,你可以这样做。为 collectionView:numberOfItemsInSection: 返回一个非常大的数字,在 collectionView:cellForItemAtIndexPath: 中,您使用 mod 运算符始终为 indexPath.Row 返回一个介于 0 和数据数组中最后一个索引之间的数字。因此,如果您的数组中有 20 个项目,您将执行以下操作:

item.whatever.text = [self.theData objectAtIndex:indexPath.row %20];
于 2013-03-20T17:07:15.927 回答
-1

感谢 rdelmar 提供了一个棘手的解决方案。“collectionView:numberOfItemsInSection 的大量数字”的想法就像一个魅力。我试图用 UIScrollView 委托方法实现相同的功能。但输出很生涩。

这是我的代码的一部分。

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {

return 100;}

// 单元格换行

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

// Setup cell identifier
static NSString *cellIdentifier = @"cvCell";


CVCell *cell;
        cell = (CVCell *)[collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];

        [cell.titleLabel setText:[self.dataArray objectAtIndex:(indexPath.row)%self.dataArray.count]];

return cell;

}

// 选择

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{


NSLog(@"Index path is %d", indexPath.row%self.dataArray.count);

}

这将产生无限滚动效果。要启用两侧无限滚动,请在 ViewWillAppear 中使用 ScrolltoIndexpath:(middle of Itemscount) !希望这将帮助即将构建无限滚动 UICollectionView 的人。

于 2013-12-12T05:35:58.343 回答
-1

覆盖此方法以启用双向无限滚动。每当它离中心太远并且用户永远不会到达终点时,它就会使内容居中。

-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
CGPoint currentOffset = self.collectionView.contentOffset;
CGFloat contentWidth = self.collectionView.contentSize.width;
CGFloat centerOffsetX = (contentWidth - self.collectionView.bounds.size.width) / 2.0;
CGFloat distanceFromCenter = fabs(currentOffset.x - centerOffsetX);

if (distanceFromCenter > (contentWidth / 4.0)) {
    self.collectionView.contentOffset = CGPointMake(centerOffsetX, currentOffset.y);
}

}
于 2016-11-03T14:35:25.160 回答