iOS开发UICollectionView 跳转到指定 item 解决方法

今天在自定义控件过程中需要解决 collectionView 跳转到指定 item 的功能,在此记录下两种方法和效果差异

方式一:滚动到指定 item

[self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:indexPath.item inSection:0] atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];

方式二:计算偏移量

CGFloat offsetX = indexPath.item * self.collectionView.frame.size.width;

[self.collectionView setContentOffset:CGPointMake(offsetX, 0)];

比较两种方式的效果:

方式一:

方式二:

对比两种效果,可以看出:

方式一是从当前 item 滚动到目标 item,会经过中间的item;

方式二则是一种跳转的效果,不会经过中间的item。

猜你喜欢

转载自blog.csdn.net/ljw2017/article/details/83088426