- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
在 Apple 的 Photo 应用程序中,如果您在滚动到任意偏移量的同时旋转设备,则之前位于中心的同一单元格将在旋转后最终位于中心。
我正在尝试使用 UICollectionView 实现相同的行为。 'indexPathsForVisibleItems' 似乎是这样做的方式......
以下代码提供了旋转之间的平滑操作,但中心项目计算似乎已关闭:
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransition(to: size, with: coordinator)
self.collectionView?.collectionViewLayout.invalidateLayout()
let middleItem = (self.collectionView?.indexPathsForVisibleItems.count / 2) - 1
let indexPath = self.collectionView?.indexPathsForVisibleItems[middleItem]
coordinator.animate(alongsideTransition: { ctx in
self.collectionView?.layoutIfNeeded()
self.collectionView?.scrollToItem(at: indexPath!, at: .centeredVertically, animated: false)
}, completion: { _ in
})
}
不使用“indexPathsForVisibleItems”,而只使用“contentOffset”?计算offset旋转后offset?但是,当旋转后的 contentSize 可能与对不同单元格大小、单元格间距等的预期不同时,这怎么可能呢?
最佳答案
您可以通过实现 UICollectionViewDelegate 来做到这一点方法 collectionView(_:targetContentOffsetForProposedContentOffset:)
:
During layout updates, or when transitioning between layouts, the collection view calls this method to give you the opportunity to change the proposed content offset to use at the end of the animation. You might return a new value if the layout or animations might cause items to be positioned in a way that is not optimal for your design.
或者,如果您已经子类化了 UICollectionViewLayout,您可以实现 targetContentOffset(forProposedContentOffset:)
在您的布局子类中,这可能更方便。
在该方法的实现中,计算并返回将导致中心单元格位于 Collection View 中心的内容偏移量。如果您的单元格大小是固定的,那么撤消由其他 UI 元素(例如状态栏和/或导航栏的消失框架)引起的内容偏移更改应该是一件简单的事情。
如果您的单元格大小因设备方向而异:
indexPathForItem(at:)
获取并保存中心单元格的索引路径在您的 Collection View 上,将内容偏移量(加上 Collection View 可见边界高度的一半)作为点传递。targetContentOffset(forProposedContentOffset:)
方法之一。通过使用保存的索引路径调用 layoutAttributesForItem(at:)
来检索中心单元格的布局属性。目标内容偏移量是该项目的框架中间(小于 Collection View 可见边界高度的一半)。第一步可以在您的 View Controller 中的 viewWillTransition(to:with:)
或 scrollViewDidScroll()
中实现。它也可以在 prepareForAnimatedBoundsChange()
中的布局对象中实现,或者在检查由设备方向变化引起的边界变化后在 invalidateLayout(with:)
中实现。 (您还需要确保 shouldInvalidateLayout(forBoundsChange:)
在这些情况下返回 true
。)
您可能需要针对内容插入、单元格间距或其他特定于您的应用的事项进行调整。
关于ios - 设备旋转后的 UICollectionView contentOffset,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41639968/
我正在尝试了解 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 文件和相应
我是一名优秀的程序员,十分优秀!