本文介绍了在旋转界面方向时将 contentOffset 保持在 UICollectionView 中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试处理 UICollectionViewController 中的界面方向更改.我想要实现的是,我希望在界面旋转后拥有 same contentOffset.意思是,应该根据边界变化的比例来改变它.

I'm trying to handle interface orientation changes in a UICollectionViewController. What I'm trying to achieve is, that I want to have the same contentOffset after an interface rotation. Meaning, that it should be changed corresponding to the ratio of the bounds change.

从纵向开始,内容偏移量为 {bounds.size.width * 2, 0} ...

Starting in portrait with a content offset of {bounds.size.width * 2, 0} …

... 应该导致横向内容偏移也与 {bounds.size.width * 2, 0} (反之亦然).

… should result to the content offset in landscape also with {bounds.size.width * 2, 0} (and vice versa).

计算新的偏移量不是问题,但不知道在哪里(或何时)设置它,以获得平滑的动画.我这样做是使 willRotateToInterfaceOrientation:duration: 中的布局无效并重置 didRotateFromInterfaceOrientation::

Calculating the new offset is not the problem, but don't know, where (or when) to set it, to get a smooth animation. What I'm doing so fare is invalidating the layout in willRotateToInterfaceOrientation:duration: and resetting the content offset in didRotateFromInterfaceOrientation::

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
                                duration:(NSTimeInterval)duration;
{
    self.scrollPositionBeforeRotation = CGPointMake(self.collectionView.contentOffset.x / self.collectionView.contentSize.width,
                                                    self.collectionView.contentOffset.y / self.collectionView.contentSize.height);
    [self.collectionView.collectionViewLayout invalidateLayout];
}

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation;
{
    CGPoint newContentOffset = CGPointMake(self.scrollPositionBeforeRotation.x * self.collectionView.contentSize.width,
                                           self.scrollPositionBeforeRotation.y * self.collectionView.contentSize.height);
    [self.collectionView newContentOffset animated:YES];
}

这会改变旋转后的内容偏移量.

This changes the content offset after the rotation.

如何在旋转过程中设置它?我尝试在 willAnimateRotationToInterfaceOrientation:duration: 中设置新的内容偏移量,但这会导致非常奇怪的行为.

How can I set it during the rotation? I tried to set the new content offset in willAnimateRotationToInterfaceOrientation:duration: but this results into a very strange behavior.

可以在我的项目中找到一个示例 GitHub.

An example can be found in my Project on GitHub.

推荐答案

您可以在视图控制器中执行此操作:

You can either do this in the view controller:

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
    super.viewWillTransition(to: size, with: coordinator)

    guard let collectionView = collectionView else { return }
    let offset = collectionView.contentOffset
    let width = collectionView.bounds.size.width

    let index = round(offset.x / width)
    let newOffset = CGPoint(x: index * size.width, y: offset.y)

    coordinator.animate(alongsideTransition: { (context) in
        collectionView.reloadData()
        collectionView.setContentOffset(newOffset, animated: false)
    }, completion: nil)
}

或者在布局本身:https://stackoverflow.com/a/54868999/308315

这篇关于在旋转界面方向时将 contentOffset 保持在 UICollectionView 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 19:00