iOS UICollectionView 在滾動時停在某個item位置上


方法一:實現UIScrollView的代理,然后實現下面這個方法

#pragma mark - UIScrollViewDelegate
//預計出大概位置,經過精確定位獲得准備位置
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset{
CGPoint targetOffset = [self nearestTargetOffsetForOffset:*targetContentOffset];
targetContentOffset->x = targetOffset.x;
targetContentOffset->y = targetOffset.y;
}
//計算落在哪個item上
- (CGPoint)nearestTargetOffsetForOffset:(CGPoint)offset
{
CGFloat pageSize = itemSpacing + itemWidth;
//四舍五入
NSInteger page = roundf(offset.x / pageSize);
CGFloat targetX = pageSize * page;
return CGPointMake(targetX, offset.y);
}

方法二:重寫UICollectionViewFlowLayout

參考文獻:http://tech.glowing.com/cn/practice-in-uiscrollview/
---------------------
作者:C_calary
來源:CSDN
原文:https://blog.csdn.net/C_calary/article/details/78891724
版權聲明:本文為博主原創文章,轉載請附上博文鏈接!

 

 

 

UICollectionView橫向移動時,且UICollectionView.page = yes只顯示一個Cell 移動時姑且計算划至第幾個cell

#pragma mark - collectionView

- (UICollectionView *)collectionView

{

    if (!_collectionView) {

        

        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];

        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;

        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, screenHeight - kTopHeight - Height_BottomView) collectionViewLayout:layout];

        

        _collectionView.dataSource = self;

        _collectionView.delegate = self;

        _collectionView.pagingEnabled = YES;

        

//        _collectionView.scrollEnabled = !GetAnswernextque; // 如果只能滑動至下一題取消滾動效果

        [_collectionView registerClass:[AnswerCollectionViewCell class] forCellWithReuseIdentifier:@"AnswerCollectionViewCell"];

        

        _collectionView.backgroundColor = [UIColor whiteColor];

        _collectionView.showsVerticalScrollIndicator = NO;

        _collectionView.showsHorizontalScrollIndicator = NO;

        

        [self.view addSubview:_collectionView];

    }

    return _collectionView;

}

 

#pragma mark - UICollectionViewDelegate

//預計出大概位置,經過精確定位獲得准備位置

- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset{

    CGPoint targetOffset = [self nearestTargetOffsetForOffset:*targetContentOffset];

    targetContentOffset->x = targetOffset.x;

    targetContentOffset->y = targetOffset.y;

//    self.currentIndexPath

    NSInteger index = targetOffset.x/screenWidth;

    DLog(@"---------%ld",index);

    self.currentIndexPath = [NSIndexPath indexPathForItem:index inSection:0];

    self.lbPageIndex.text = [NSString stringWithFormat:@"%li/%li",self.currentIndexPath.item,self.dataSource.count];

}

//計算落在哪個item上

- (CGPoint)nearestTargetOffsetForOffset:(CGPoint)offset

{

    CGFloat pageSize = screenWidth;

    //四舍五入

    NSInteger page = roundf(offset.x / pageSize);

    CGFloat targetX = pageSize * page;

    return CGPointMake(targetX, offset.y);

}

 

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {

    CGPoint offsetPoint = scrollView.contentOffset;

    self.currentIndexPath = [NSIndexPath indexPathForItem:offsetPoint.x/(screenWidth) inSection:0];

    self.lbPageIndex.text = [NSString stringWithFormat:@"%li/%li",self.currentIndexPath.item,self.dataSource.count];

}

 

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {

    CGPoint offsetPoint = scrollView.contentOffset;

    self.currentIndexPath = [NSIndexPath indexPathForItem:offsetPoint.x/(screenWidth) inSection:0];

    self.lbPageIndex.text = [NSString stringWithFormat:@"%li/%li",self.currentIndexPath.item,self.dataSource.count];

}

 

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {

    CGPoint offsetPoint = scrollView.contentOffset;

    self.currentIndexPath = [NSIndexPath indexPathForItem:offsetPoint.x/(screenWidth) inSection:0];

    self.lbPageIndex.text = [NSString stringWithFormat:@"%li/%li",self.currentIndexPath.item,self.dataSource.count];

    

}

#pragma mark - FlipPageDelegate

- (void)flipToNextPage{

    CGPoint offsetPoint = self.collectionView.contentOffset;

    NSInteger pageIndex = offsetPoint.x / screenWidth;

    if (pageIndex < self.dataSource.count - 1) {

        pageIndex++;

        NSIndexPath *curIndexPath = [NSIndexPath indexPathForItem:pageIndex inSection:0];

        self.currentIndexPath = curIndexPath;

        self.lbPageIndex.text = [NSString stringWithFormat:@"%li/%li",pageIndex,self.dataSource.count];

        NSIndexPath *indexPath = [NSIndexPath indexPathForItem:pageIndex inSection:0];

        [self.collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];

    }else{

        [ToolUtils showSuccess:@"已經是最后一頁"];

    }

}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM