Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/flash/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios scrollToItemAtIndexPath工作不正常_Ios_Objective C_Uicollectionview_Autorotate - Fatal编程技术网

Ios scrollToItemAtIndexPath工作不正常

Ios scrollToItemAtIndexPath工作不正常,ios,objective-c,uicollectionview,autorotate,Ios,Objective C,Uicollectionview,Autorotate,我正在尝试使用UICollectionView实现一个照片库。设置与此类似:单元格与集合视图一样大,因此一次只能看到一张图片。分页已启用,因此您可以逐个图片滚动浏览库。到目前为止一切正常 我还希望在设备旋转到横向时保持该设置。它在单元格/图像大小方面工作正常。但正如前面的教程中所描述的,集合视图在两张图片之间旋转到某个奇怪的位置 我的目标是让集合视图在旋转后显示与旋转前相同的单元格。就像这样 我解决此问题的尝试: 在旋转之前,我将当前可见项的indepath保存到属性,如下所示: -(void)

我正在尝试使用
UICollectionView
实现一个照片库。设置与此类似:单元格与集合视图一样大,因此一次只能看到一张图片。分页已启用,因此您可以逐个图片滚动浏览库。到目前为止一切正常

我还希望在设备旋转到横向时保持该设置。它在单元格/图像大小方面工作正常。但正如前面的教程中所描述的,集合视图在两张图片之间旋转到某个奇怪的位置

我的目标是让集合视图在旋转后显示与旋转前相同的单元格。就像这样

我解决此问题的尝试:

在旋转之前,我将当前可见项的
indepath
保存到属性,如下所示:

-(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];
}
不幸的是,这只适用于集合中的前两项,如果我滚动到第五项并旋转设备,它会再次旋转到单元格之间的某个奇怪位置


知道我做错了什么吗?

我在iOS 6上遇到了完全相同的问题,iOS 7上也解决了这个问题。以下是适用于iOS 6的变通方法

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
    [super didRotateFromInterfaceOrientation:fromInterfaceOrientation];

    // workaround iOS 6 collection view rotation bug: UICollectionView is not scrolled to the correct index after rotation    
    [self.collectionView setContentOffset:[self.collectionView.collectionViewLayout layoutAttributesForItemAtIndexPath:[NSIndexPath indexPathForRow:self.currentPage inSection:0]].frame.origin animated:NO];
}

我不知道什么对你不起作用,对我来说它似乎应该起作用,但是我想建议你使用现成的库来显示照片库,因为这是一项如此常见的任务,有如此优秀的代码,并根据需要折射它。我做过很多项目,其中包括一个图库,对人们经常使用的“经过战斗的”代码进行折射通常是很省时的,例如MWphotoBrowser你找到了解决问题的方法吗?我在iOS 6上也遇到了同样的问题。但它在iOS 7中是固定的。