- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我有一个启用了分页的 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) {
}];
}
但考虑到我的照片是水平滚动的。
此过渡期间的动画不是很流畅,但可以接受。如果你想让它变得优秀,你可以在旋转之前隐藏一个 collectionView 并在屏幕上放置一个带有当前图像的 ImageView 。 ImageView 应该可以毫无问题地旋转。旋转后,您可以从上面运行适当的代码,然后从屏幕上删除 ImageView 。我还没有尝试实现这个想法,但它应该看起来像:
- (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
}];
}
抱歉, objective-c :)。
关于ios - 旋转后如何调整UICollectionView contentOffset?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35847278/
我正在尝试了解 Azure 认知搜索中的技能组。我想构建一个 Ocr 支持的搜索,并尝试了解它是如何工作的。 例如documentation说 ocr 技能 产生响应: { "text": "He
我正在为 tvOS 开发一个小型应用程序,我需要在其中显示 UITextView。 不幸的是,有时此文本无法容纳在屏幕内,这就是为什么我需要一种以编程方式滚动到其底部的方法。 我尝试使用以下代码: s
所以我有以下代码: int actualPosition = self.collectionView.contentOffset.y; NSLog(@"%i", actualPosition); 输出
我制作了一个带有几个按钮的 .xib 文件,并将其附加到 inputAccessoryView。我使用scrollView使该部分可滚动,但是我必须使用按钮(箭头)添加附加功能。单击向前箭头,它应该转
我有一个按钮可以改变 scrollOffset 的颜色。我有下面的代码工作,除了它在相反的方向执行动画。我不希望它从透明变为不透明 (0% - 100%),而是相反 (100% - 0%)。我想我计算
我有一个启用了分页的 UICollectionView,旋转后页面 contentOffset 没有正确调整。 我重写了下面两个方法 override func viewWillTransitionT
我试图在 setContentOffset 动画中间获取 UIScrollView 的 contentOffset 属性。请注意,我没有使用该方法的动画属性,而是将 setContentOffset
这是一个非常简单的问题,真的: 如果我告诉我的UIScrollView设置其contentOffset类似于 {0, 50} ,然后将其设置为 {0, 100}片刻之后,它的最终位置会是y = 100
看到围绕更改 ScrollView 的内容偏移属性的奇怪问题。 我有一个CADisplayLink,它每帧调用一个方法。该方法计算调整内容偏移量以产生自动滚动类型的效果。 @objc private
我有一个 UIScrollView,只要点击 View ,它就会重置其 contentOffset。 这在此处进行了演示:https://gyazo.com/c8fb5c0fa5b6d53acf45c
我有一个带分页的 ScrollView 。我声明: if(_fullPosterScroll == nil) _fullPosterScroll = [[UIScrollView alloc]
当用户拖动它或系统为其滚动设置动画时,我需要从 UIScrollView 获取 x 偏移量。 我正在使用 - (void)scrollViewDidScroll:(UIScrollView *)scr
我正在开发 iPhone 应用程序。我的代码是这样实现的: RootViewController 将 UIScrollView 作为其 View 。然后 RootViewController 推送另一
我正在向我的 UITableView 添加一个 UISearchBar。与许多应用程序一样,搜索栏最初会隐藏在导航栏后面,直到用户向上滚动,才会显示出来。我使用下面的代码最初隐藏搜索栏,当表格 Vie
我有一个 UITableView,我想隐藏它的一部分标题(有点像拉动刷新)。我用 contentOffset 做了它,当内容大于屏幕时它工作正常。它在需要时隐藏并在需要时显示,但是当内容占用的空间小于
我想在用户滚动时更改 View 的框架。我正在检查 scrollViewDidScroll 中 ScrollView 的 contentOffSet,我想知道 contentOffSet 何时达到特定
我在 UIViewController 中有一个 UITableView 作为 subview 。当用户按下 tableView 下的 UITextfield 时,我在键盘出现时设置 tableVie
我有一个大的水平 ScrollView ,上面有不同的 View 。滚动工作正常,但现在我添加了按钮以转到下一页的上一页。我认为这段代码可以解决问题,因为我已经用过几次了: -(void)goBack
我正在使用计时器并打印 UIScrollView 的 contentOffset.y 以一直跟踪它。 但问题是 UIScrollView 仅在减速后更新其 contentOffset 值。 我想知道
我有一个 UIViewController,它有多个从屏幕外进入的 UIView。我没有在 UIViewController 中初始化和设计所有这些 UIView,而是创建了单独的 nib 文件和相应
我是一名优秀的程序员,十分优秀!