collectionView点击实现滑动

通过点击来实现collectionView的滑动
有系统的方法可以实现
scrollToItemAtIndexPath: atScrollPosition: animated:

scrollPosition这个参数控制cell具体停留在上下左右中到底哪个位置

UICollectionViewScrollPositionNone                 = 0,
    
// The vertical positions are mutually exclusive to each other, but are bitwise or-able with the horizontal scroll positions.
// Combining positions from the same grouping (horizontal or vertical) will result in an NSInvalidArgumentException.
    UICollectionViewScrollPositionTop                  = 1 << 0,
    UICollectionViewScrollPositionCenteredVertically   = 1 << 1,
    UICollectionViewScrollPositionBottom               = 1 << 2,
    
// Likewise, the horizontal positions are mutually exclusive to each other.
    UICollectionViewScrollPositionLeft                 = 1 << 3,
    UICollectionViewScrollPositionCenteredHorizontally = 1 << 4,
    UICollectionViewScrollPositionRight                = 1 << 5

例如我写的collectionView为横向滑动,需要点击两边cell都可以左右滑动,代码如下

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    [collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
    
}
gif5新文件.gif

你可能感兴趣的:(collectionView点击实现滑动)