Gre*_*reg 8 ios uicollectionview uicollectionviewlayout
任何想法如何解决滚动whist旋转时我的UICollectionView崩溃的事实?
我使用以下方法分别滚动和旋转,每个似乎工作正常.我刚刚注意到在做这两件事的同时我可以得到这个崩溃.因此,当我旋转设备并且在prepareLayout中计算新布局属性时,似乎与事实有关,即连续滚动触发"invalidateLayoutWithContext(invalidContext)"(见下文).
想法?是否有办法在旋转期间将任何滚动响应置于保持状态(或忽略它们)?
旋转方法 在视图控制器中的viewWillLayoutSubviews中,我使整个布局无效
self.cal.collectionViewLayout.invalidateLayout()
Run Code Online (Sandbox Code Playgroud)
滚动方法 为了让我有一个"粘性"装饰视图(标题)我不会使整个布局无效,因为它会导致性能下降,但请执行以下操作.在布局类中,我重写了shouldInvalidateLayoutForBoundsChange
override func shouldInvalidateLayoutForBoundsChange(newBounds: CGRect) -> Bool {
let invalidContext : UICollectionViewLayoutInvalidationContext = self.invalidationContextForBoundsChange(newBounds)
// Keep Header Sticky
invalidContext.invalidateDecorationElementsOfKind(GCCalendarLayoutKind_Decorative1, atIndexPaths: [headerDecorativeIndexPath])
// Apply Invalidation
self.invalidateLayoutWithContext(invalidContext)
// Return normal super return (just in case of future IOS upgrades)
return super.shouldInvalidateLayoutForBoundsChange(newBounds)
}
Run Code Online (Sandbox Code Playgroud)
请注意,我将装饰视图(标题)无效,而崩溃的错误是关于我的补充视图布局不同.
错误
2015-10-30 07:14:30.181 test3_collectionview [17086:3102132] *断言失败 - [UICollectionViewData validateLayoutInRect:],/ BuildRoot / Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit -3512.29.5/UICollectionViewData.m:408 2015-10-30 07:14:30.185 test3_collectionview [17086:3102132]* 由于未捕获的异常'NSInternalInconsistencyException'而终止应用程序,原因:'索引路径上的补充项的布局属性({length = 2,path = 0 - 0})从索引路径改变:({length = 2,path = 0 - 0}); 元素类:(装饰1); frame =(0 1085.5; 320 16); zIndex = 1; 索引路径:({length = 2,path = 0 - 0}); 元素类:(装饰1); frame =(0 853.5; 320 16); zIndex = 1; 没有使布局无效'***First throw call stack:
我不确定这是否足够,但我会尝试一下这些方法(集合视图是滚动视图):
import CoreGraphics
class myController: UIViewController, UIScrollViewDelegate {
var myScrollView = UIScrollView()
override func viewDidLoad() {
self.view.addSubview(myScrollView)
}
// This will capture rotation events
override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
self.myScrollView.scrollEnabled = false
coordinator.notifyWhenInteractionEndsUsingBlock( {_ in self.myScrollView.scrollEnabled = true} )
super.viewWillTransitionToSize(size, withTransitionCoordinator: coordinator)
}
func scrollViewDidScroll(scrollView: UIScrollView) {
// This will not ignore scroll but perhaps can help keeping things "tidy" and "performant" during rotation. Not sure
if myScrollView.scrollEnabled == false {
let offset = scrollView.contentOffset
myScrollView.setContentOffset(offset, animated: false)
}
}
}
Run Code Online (Sandbox Code Playgroud)