大佬教程收集整理的这篇文章主要介绍了ios – 滚动双向滚动UICollectionView并具有大量单元格(250,000或更多)时可见延迟,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
import UIKit class LuckGameCollectionViewLayout: UICollectionViewFlowLayout { // Used for calculaTing each cells CGRect on screen. // CGRect will define the Origin and Size of the cell. let CELL_HEIGHT = 70.0 let CELL_WIDTH = 70.0 // Dictionary to hold the UICollectionViewLayoutAttributes for // each cell. The layout attribtues will define the cell's size // and position (x,y,and z indeX). I have found this process // to be one of the heavier parts of the layout. I recommend // holding onto this data after it has been calculated in either // a Dictionary or data store of some kind for a smooth perfoRMANce. var cellAttrsDictionary = Dictionary<NSIndexPath,UICollectionViewLayoutAttributes>() // Defines the size of the area the user can move around in // within the collection view. var contentSize = CGSize.zero override func collectionViewContentSize() -> CGSize { return self.contentSize } override func prepareLayout() { // Cycle through each section of the data source. if collectionView?.numberOfSections() > 0 { for section in 0...collectionView!.numberOfSections()-1 { // Cycle through each item in the section. if collectionView?.numberOfItemsInSection(section) > 0 { for item in 0...collectionView!.numberOfItemsInSection(section)-1 { // Build the UICollectionVieLayoutAttributes for the cell. let cellIndex = NSIndexPath(forItem: item,inSection: section) let xPos = Double(item) * CELL_WIDTH let yPos = Double(section) * CELL_HEIGHT let cellAttributes = UICollectionViewLayoutAttributes(forCellWithIndexPath: cellIndeX) cellAttributes.frame = CGRect(x: xPos,y: yPos,width: CELL_WIDTH,height: CELL_HEIGHT) // Save the attributes. cellAttrsDictionarY[cellIndex] = cellAttributes } } } } // update content size. let contentWidth = Double(collectionView!.numberOfItemsInSection(0)) * CELL_WIDTH let contentHeight = Double(collectionView!.numberOfSections()) * CELL_HEIGHT self.contentSize = CGSize(width: contentWidth,height: contentHeight) } override func layoutAttributesForElementsInRect(rect: CGRect) -> [UICollectionViewLayoutAttributes]? { // Create an array to hold all elements found in our current view. var attributesInRect = [UICollectionViewLayoutAttributes]() // check each element to see if it should be returned. for (_,cellAttributes) in cellAttrsDictionary { if CGRecTintersectsRect(rect,cellAttributes.framE) { attributesInRect.append(cellAttributes) } } // Return list of elements. return attributesInRect } override func layoutAttributesForItemATindexPath(indexPath: NSIndexPath) -> UICollectionViewLayoutAttributes? { return cellAttrsDictionarY[indexPath]! } override func shouldInvalidateLayoutForBoundsChange(newBounds: CGRect) -> Bool { return false } }
编辑:
以下是我在layoutAttributesForElementsInRect方法中提出的更改.
override func layoutAttributesForElementsInRect(rect: CGRect) -> [UICollectionViewLayoutAttributes]? { // Create an array to hold all elements found in our current view. var attributesInRect = [UICollectionViewLayoutAttributes]() let xOffSet = self.collectionView?.contentOffset.x let yOffSet = self.collectionView?.contentOffset.y let @R_678_10586@lcolumnCount = self.collectionView?.numberOfSections() let @R_678_10586@lRowCount = self.collectionView?.numberOfItemsInSection(0) let startRow = Int(Double(xOffSet!)/CELL_WIDTH) - 10 //include 10 rows toWARDs left let endRow = Int(Double(xOffSet!)/CELL_WIDTH + Double(Utils.getScreenWidth())/CELL_WIDTH) + 10 //include 10 rows toWARDs right let startCol = Int(Double(yOffSet!)/CELL_HEIGHT) - 10 //include 10 rows toWARDs top let endCol = Int(Double(yOffSet!)/CELL_HEIGHT + Double(Utils.getScreenHeight())/CELL_HEIGHT) + 10 //include 10 rows toWARDs bottom for(var i = startRow ; i <= endRow; i = i + 1){ for (var j = startCol ; j <= endCol; j = j + 1){ if (i < 0 || i > (@R_678_10586@lRowCount! - 1) || j < 0 || j > (@R_678_10586@lcolumnCount! - 1)){ conTinue } let indexPath: NSIndexPath = NSIndexPath(forRow: i,inSection: j) attributesInRect.append(cellAttrsDictionarY[indexPath]!) } } // Return list of elements. return attributesInRect }
我已经计算了collectionView的偏移量,并用它来计算屏幕上可见的单元格(使用每个单元格的高度/宽度).我不得不在每一侧添加额外的单元格,以便当用户滚动时没有丢失的单元格.我测试了这个并且性能很好.
编辑:我添加了更大的Result,以便在滚动到达之前可以预先计算属性.从您的编辑看起来,您仍然在缓存我认为性能不需要的属性.此外,它会带来更大的内存占用,滚动更多.还有一个原因是你不想从回调中使用提供的CGRect而不是从contentOffset手动计算一个?
class LuckGameCollectionViewLayout: UICollectionViewFlowLayout { let CELL_HEIGHT = 50.0 let CELL_WIDTH = 50.0 override func collectionViewContentSize() -> CGSize { let contentWidth = Double(collectionView!.numberOfItemsInSection(0)) * CELL_WIDTH let contentHeight = Double(collectionView!.numberOfSections()) * CELL_HEIGHT return CGSize(width: contentWidth,height: contentHeight) } override func layoutAttributesForElementsInRect(rect: CGRect) -> [UICollectionViewLayoutAttributes]? { let biggerRect = rect.insetBy(dx: -2048,dy: -2048) let starTindexY = Int(Double(biggerRect.origin.y) / CELL_HEIGHT) let starTindexX = Int(Double(biggerRect.origin.X) / CELL_WIDTH) let numberOfVisibleCellsInRectY = Int(Double(biggerRect.height) / CELL_HEIGHT) + starTindexY let numberOfVisibleCellsInRectX = Int(Double(biggerRect.width) / CELL_WIDTH) + starTindexX var attributes: [UICollectionViewLayoutAttributes] = [] for section in starTindexY..<numberOfVisibleCellsInRectY where section >= 0 && section < self.collectionView!.numberOfSections() { for item in starTindexX..<numberOfVisibleCellsInRectX where item >= 0 && item < self.collectionView!.numberOfItemsInSection(section) { let cellIndex = NSIndexPath(forItem: item,inSection: section) if let attrs = self.layoutAttributesForItemATindexPath(cellIndeX) { attributes.append(attrs) } } } return attributes } override func layoutAttributesForItemATindexPath(indexPath: NSIndexPath) -> UICollectionViewLayoutAttributes? { let xPos = Double(indexPath.row) * CELL_WIDTH let yPos = Double(indexPath.section) * CELL_HEIGHT let cellAttributes = UICollectionViewLayoutAttributes(forCellWithIndexPath: indexPath) cellAttributes.frame = CGRect(x: xPos,height: CELL_HEIGHT) return cellAttributes } }
以上是大佬教程为你收集整理的ios – 滚动双向滚动UICollectionView并具有大量单元格(250,000或更多)时可见延迟全部内容,希望文章能够帮你解决ios – 滚动双向滚动UICollectionView并具有大量单元格(250,000或更多)时可见延迟所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。