UICollectionView监听滚动距离及获取当前index
2019-05-08 本文已影响0人
独孤流
前言
由于开发中遇到一个既有按钮可以点击展示上一个和下一个,也可以手动滑动展示上一个下一个,导致监听UICollectionView到底展示的是哪个indexPath比较麻烦,特记录一下
参考:
监听滚动停止:
注意:页面中点击上一个、下一个按钮调用的
- (void)scrollToItemAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UICollectionViewScrollPosition)scrollPosition animated:(BOOL)animated;
不会走这个滚动的回调
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
// 停止类型1、停止类型2
BOOL scrollToScrollStop = !scrollView.tracking && !scrollView.dragging && !scrollView.decelerating;
if (scrollToScrollStop) {
[self scrollViewDidEndScroll];
}
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
if (!decelerate) {
// 停止类型3
BOOL dragToDragStop = scrollView.tracking && !scrollView.dragging && !scrollView.decelerating;
if (dragToDragStop) {
[self scrollViewDidEndScroll];
}
}
}
#pragma mark - scrollView 停止滚动监测
- (void)scrollViewDidEndScroll {
NSArray *indexPaths = [self.collectionView indexPathsForVisibleItems];
NSIndexPath *indexPath = indexPaths.firstObject;
// 将collectionView在控制器view的中心点转化成collectionView上的坐标
// CGPoint pInView = [self convertPoint:self.collectionView.center toView:self.collectionView];
// // 获取这一点的indexPath
// NSIndexPath *indexPath = [self.collectionView indexPathForItemAtPoint:pInView];
}
调用UICollectionView的reloadData
后立刻调用scrollToItemAtIndexPath...
,则scrollToItemAtIndex不会起作用,需要在relaodData后延迟一下调用
- (void)awakeFromNib {
[super awakeFromNib];
self.selectionStyle = UITableViewCellSelectionStyleNone;
[self.collectionView registerNib:[UINib nibWithNibName:@"xxxxCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"xxxxCollectionViewCell"];
self.collectionView.backgroundColor = [UIColor whiteColor];
self.collectionView.dataSource = self;
self.collectionView.delegate = self;
self.collectionView.scrollEnabled = YES;
self.collectionView.pagingEnabled = YES;
self.collectionView.showsHorizontalScrollIndicator = NO;
self.selectionStyle = UITableViewCellSelectionStyleNone;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return self.dataList.count;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return CGSizeMake(Main_Screen_Width, collectionView.height);
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
return UIEdgeInsetsMake(0, 0, 0, 0);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
return 0;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
return 0;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
xxxxCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"xxxxCollectionViewCell" forIndexPath:indexPath];
XXXModel *model = self.dataList[indexPath.item];
cell.model = model;
return cell;
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
// 停止类型1、停止类型2
BOOL scrollToScrollStop = !scrollView.tracking && !scrollView.dragging && !scrollView.decelerating;
if (scrollToScrollStop) {
[self scrollViewDidEndScroll];
}
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
if (!decelerate) {
// 停止类型3
BOOL dragToDragStop = scrollView.tracking && !scrollView.dragging && !scrollView.decelerating;
if (dragToDragStop) {
[self scrollViewDidEndScroll];
}
}
}
#pragma mark - scrollView 停止滚动监测
- (void)scrollViewDidEndScroll {
NSArray *indexPaths = [self.collectionView indexPathsForVisibleItems];
NSIndexPath *indexPath = indexPaths.firstObject;
// 将collectionView在控制器view的中心点转化成collectionView上的坐标
// CGPoint pInView = [self convertPoint:self.collectionView.center toView:self.collectionView];
// // 获取这一点的indexPath
// NSIndexPath *indexPath = [self.collectionView indexPathForItemAtPoint:pInView];
self.selectIndex = indexPath.item;
[self updateShowTitle];
}
- (IBAction)preBtnClicked:(id)sender {
self.selectIndex -=1;
[self updateShow];
}
- (IBAction)nextBtnClicked:(id)sender {
self.selectIndex +=1;
[self updateShow];
}
- (void)setDataList:(NSArray<VIPLevelDataModel *> *)dataList
{
_dataList = dataList;
[self initUpdateShow];
}
- (void)updateShowTitle
{
if(self.dataList.count == 0){
return;
}
if(self.selectIndex == 0){
self.preBtn.enabled = NO;
}else{
self.preBtn.enabled = YES;
}
if(self.selectIndex >= self.dataList.count-1){
self.nextBtn.enabled = NO;
}else{
self.nextBtn.enabled = YES;
}
NSString *imageName = [NSString stringWithFormat:@"vip_red%ld",self.selectIndex];
self.vipImage.image = [UIImage imageNamed:imageName];
}
- (void)initUpdateShow
{
if(self.dataList.count == 0){
return;
}
[self updateShowTitle];
[self.collectionView reloadData];
WS(weakSelf)
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:self
.selectIndex inSection:0];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[weakSelf.collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:NO];
});
}
- (void)updateShow
{
if( self.dataList.count == 0){
return;
}
[self updateShowTitle];
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:self.selectIndex inSection:0];
[self.collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
}