自动使UICollectionview水平滚动

时间:2019-01-09 11:46:56

标签: ios uicollectionview

我正在设置一个集合视图,其中包含许多单元格,并且已经水平滚动了,以自动滚动每个单元格,这是我到目前为止发现的,这是使用计时器完成的,但是应该做些什么集合视图移动了吗?当到达最后一个牢房时,从第一个牢房重新开始。

我尝试过:

-(void)fireAlert {
NSTimer *timer = [NSTimer timerWithTimeInterval:2.0 target:self selector:@selector(scrollCollectionView) userInfo:nil repeats:YES];
[timer fire];

}

-(void)scrollCollectionView{// cell index for testing, its 0 at the beginning.
self.cellIndex = self.cellIndex + 1;
[self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:5 inSection:0]
                            atScrollPosition:UICollectionViewScrollPositionBottom
                                    animated:YES];
   }

但是没用

其他有用的代码:

-(void)setCollectionView {
self.collectionView.delegate = self;
self.collectionView.dataSource = self;
[self.collectionView setPagingEnabled:YES];
[self setCollectionViewCell];
self.collectionView.backgroundColor = [UIColor blackColor];
}



-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
   // filling the cells
return cell;
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
return CGSizeMake(150, self.collectionView.frame.size.height);

}

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
return UIEdgeInsetsMake(0, 0, 0, 0);
}

1 个答案:

答案 0 :(得分:0)

看看下面的GitHub回购。另一方面,它是用Swift编写的,因此请确保您必须将代码转换为Objective C

https://github.com/rmurdoch/Infinite-Auto-Scroll-UICollectionView