Ios 旋转后如何调整UICollectionView内容偏移?
我有一个Ios 旋转后如何调整UICollectionView内容偏移?,ios,swift,uiscrollview,uicollectionview,Ios,Swift,Uiscrollview,Uicollectionview,我有一个UICollectionView启用了分页,页面contentOffset在旋转后没有正确调整 我重写了以下两个方法 override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) { collectionView.collectionViewLayout.invalidateLayout(
UICollectionView
启用了分页,页面contentOffset
在旋转后没有正确调整
我重写了以下两个方法
override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
collectionView.collectionViewLayout.invalidateLayout()
}
override func didRotateFromInterfaceOrientation(fromInterfaceOrientation: UIInterfaceOrientation) {
let width = collectionView.bounds.width
var frame = collectionView.frame
frame.origin.x = width * CGFloat(pageControl.currentPage)
frame.origin.y = 0
collectionView.scrollRectToVisible(frame, animated: false)
}
但还是有同样的问题
当设备旋转时,需要做哪些更改才能正确调整当前页面的contentOffset
在横向上,页面位置正确,但当设备旋转至纵向时,页面位置不正确,如下图所示
我已经用下一个代码修复了这个问题:
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
{
CGPoint offset = self.collectionView.contentOffset;
CGFloat width = self.collectionView.bounds.size.width;
NSInteger index = round(offset.x / width);
CGPoint newOffset = CGPointMake(index * size.width, offset.y);
[self.collectionView setContentOffset:newOffset animated:NO];
[coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> _Nonnull context) {
[self.collectionView reloadData];
[self.collectionView setContentOffset:newOffset animated:NO];
} completion:^(id<UIViewControllerTransitionCoordinatorContext> _Nonnull context) {
}];
}
-(void)视图将转换大小:(CGSize)带有转换协调器的大小:(id)协调器
{
CGPoint offset=self.collectionView.contentOffset;
CGFloat width=self.collectionView.bounds.size.width;
NSInteger索引=圆形(偏移量x/宽度);
CGPoint newOffset=CGPointMake(索引*size.width,offset.y);
[self.collectionView setContentOffset:newOffset动画:否];
[coordinator AnimateLongsideTransition:^(id_非空上下文){
[self.collectionView-reloadData];
[self.collectionView setContentOffset:newOffset动画:否];
}完成:^(id_非空上下文){
}];
}
但要考虑到我的照片是水平滚动的
此过渡期间的动画不是很平滑,但可以接受。如果您想让它变得出色,可以在旋转之前隐藏collectionView,并将带有当前图像的图像视图放置在屏幕上。图像视图应该可以毫无问题地旋转。旋转后,您可以从上面运行适当的代码,然后从屏幕上删除图像视图。我还没有尝试实施这个想法,但它应该是这样的:
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
{
CGPoint offset = self.collectionView.contentOffset;
CGFloat width = self.collectionView.bounds.size.width;
NSInteger index = round(offset.x / width);
CGPoint newOffset = CGPointMake(index * size.width, offset.y);
//here you should create an image view and place it on the screen
//without animation, you should also make constraints for it
//you also should hide the collection view
[coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> _Nonnull context) {
} completion:^(id<UIViewControllerTransitionCoordinatorContext> _Nonnull context) {
[self.collectionView reloadData];
[self.collectionView setContentOffset:newOffset animated:NO];
//here you should remove the image view and show the collection view
}];
}
-(void)视图将转换大小:(CGSize)带有转换协调器的大小:(id)协调器
{
CGPoint offset=self.collectionView.contentOffset;
CGFloat width=self.collectionView.bounds.size.width;
NSInteger索引=圆形(偏移量x/宽度);
CGPoint newOffset=CGPointMake(索引*size.width,offset.y);
//在这里,您应该创建一个图像视图并将其放置在屏幕上
//如果没有动画,还应该对其进行约束
//您还应该隐藏集合视图
[coordinator AnimateLongsideTransition:^(id_非空上下文){
}完成:^(id_非空上下文){
[self.collectionView-reloadData];
[self.collectionView setContentOffset:newOffset动画:否];
//在这里,您应该删除图像视图并显示集合视图
}];
}
对不起,目标C:)。您所说的
contentOffset未正确调整是什么意思
。怎么了?