【问题标题】:How to adjust UICollectionView contentOffset after rotation?旋转后如何调整 UICollectionView contentOffset?
【发布时间】:2016-03-07 15:15:39
【问题描述】:

我有一个启用分页的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?

在横向页面定位正确,但当设备旋转到纵向页面位置不正确,如下图所示

【问题讨论】:

  • contentOffset is not correctly adjusted 是什么意思。怎么了?

标签: ios swift uiscrollview uicollectionview


【解决方案1】:

我已经用下一个代码解决了这个问题:

- (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) {

    }];
}

但要考虑到我的照片是水平滚动的。

此过渡期间的动画不是很流畅,但可以接受。如果你想让它变得更好,你可以在旋转之前隐藏一个 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

    }];
}

对不起目标 C :)。

【讨论】:

    猜你喜欢
    • 2017-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-09
    • 2013-03-12
    • 2015-12-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多