我正在尝试使用a实现照片库UICollectionView
.设置类似于本教程中的设置:单元格与集合视图一样大,因此您一次只能看到一张图片.分页已启用,因此您可以逐个图片滚动浏览图库.到目前为止一切正常.
当设备旋转到横向时,我还想保留该设置.它在细胞/图像大小方面工作得很好.但是就像在前面提到的教程中描述的那样,集合视图被旋转到两个图片之间的某个奇怪位置.
我的目标是让集合视图在旋转之后显示与旋转之前显示的相同的单元格.就像在这篇文章中一样.
我试图解决这个问题:
在旋转之前,我将indexpath
当前可见项目保存到属性,如下所示:
-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration { NSArray *visibleItems = [self.galleryCollectionView indexPathsForVisibleItems]; self.currentIndexPath = [visibleItems lastObject]; [self.galleryCollectionView.collectionViewLayout invalidateLayout]; }
在旋转之后,我尝试滚动到该项目,如下所示:
-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation { [self.galleryCollectionView scrollToItemAtIndexPath:self.currentIndexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES]; }
不幸的是,这只适用于集合中的前两个项目,如果我滚动到让第五个项目旋转并将设备旋转到一些奇怪的中间单元格位置.
我有什么想法我做错了吗?