我正在尝试处理 UICollectionViewController 中的界面方向更改。我想要实现的是,我想要拥有same界面旋转后的 contentOffset。意思是,它应该根据边界变化的比率进行更改。
从纵向开始,内容偏移量为 {边界.尺寸.宽度* 2, 0} …
… 也应该导致横向内容偏移 {边界.尺寸.宽度* 2, 0}(反之亦然)。
计算新的偏移量不是问题,但不知道在哪里(或何时)设置它,以获得平滑的动画。我所做的就是使布局无效willRotateToInterfaceOrientation:duration:
并重置内容偏移量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];
}
这会更改旋转后的内容偏移量。
轮换期间如何设置?我尝试将新内容偏移量设置为willAnimateRotationToInterfaceOrientation:duration:
但这会导致非常奇怪的行为。
可以在我的项目中找到一个示例GitHub https://github.com/anagromataf/TKViewControllerCollectionController/blob/develop/TKViewControllerCollectionController/TKViewControllerCollectionController.m#L86.
您可以在视图控制器中执行此操作:
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 https://stackoverflow.com/a/54868999/308315
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)