我正在尝试创建自己的图书应用程序,并使用UICollectionView
列出所有的图书。 每个单元格的数据来自.plist
文件,我使用了自定义的FlowLayout
(稍后进行一些更改)。
所以现在我被滚动时的延迟和滞后弄得焦头烂额。 我想我在向单元格传递数据或单元格初始化时犯了错误。
由.xib
和自定义类创建的单元格,只是一些布局和UI:
class BookCoverCell: UICollectionViewCell {
@IBOutlet weak var view1: UIView!
@IBOutlet weak var view2: UIView!
@IBOutlet weak var imageView: UIImageView!
@IBOutlet weak var darkRedView: UIView!
@IBOutlet weak var lightRedView: UIView!
@IBOutlet weak var readButton: UIButton!
@IBOutlet weak var pageLabel: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
clipsToBounds = true
self.backgroundColor = UIColor(white: 1, alpha: 0.0)
view1.layer.cornerRadius = 10
view2.layer.cornerRadius = 10
darkRedView.layer.cornerRadius = 10
lightRedView.layer.cornerRadius = 10
}
}
在ViewController的类中:
class MainVC: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UIScrollViewDelegate {
@IBOutlet weak var collectionContainerView: UIView!
@IBOutlet weak var collectionView: UICollectionView!
var books: Array<Book>? {
didSet {
collectionView.reloadData()
}
}
var flowLayout = UICollectionViewFlowLayout()
override func viewDidLayoutSubviews() {
flowLayout = ZoomAndSnapFlowLayout()
collectionView.collectionViewLayout = flowLayout
}
override func viewDidLoad() {
super.viewDidLoad()
collectionContainerView.backgroundColor = UIColor(white: 1, alpha: 0.0)
collectionView.delegate = self
collectionView.dataSource = self
collectionView.register(UINib(nibName: "BookCoverCell", bundle: nil), forCellWithReuseIdentifier: "BookCoverCell");
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(true)
books = BookStore.sharedInstance.loadBooks(plist: "Books")
}
//MARK: UICollectionViewDelegate
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if let books = books {
return books.count
}
return 0
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "BookCoverCell", for: indexPath) as! BookCoverCell
let book = books![indexPath.row]
let cover = book.coverImage()!
let color = book.getDominantColor()
cell.view1.backgroundColor = color
cell.imageView.image = cover
cell.pageLabel.text = "Pages: 29"
cell.readButton.setTitle("Read", for: .normal)
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let book = books?[indexPath.row]
print ("open")
}
}
我的布局现在看起来是:
class ZoomAndSnapFlowLayout: UICollectionViewFlowLayout {
var cellWidth = CGFloat()
var cellHeight = CGFloat()
var minLineSpacing = CGFloat()
override init() {
super.init()
self.scrollDirection = .horizontal
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func prepare() {
guard let collectionView = collectionView else { fatalError() }
cellHeight = collectionView.frame.height * 0.8
minLineSpacing = 200
cellWidth = cellHeight
itemSize = CGSize(width: cellWidth, height: cellHeight)
let verticalInsets = (collectionView.frame.height - collectionView.adjustedContentInset.top - collectionView.adjustedContentInset.bottom - itemSize.height) / 2
let horizontalInsets = (collectionView.frame.width - collectionView.adjustedContentInset.right - collectionView.adjustedContentInset.left - itemSize.width) / 2
sectionInset = UIEdgeInsets(top: verticalInsets, left: horizontalInsets, bottom: verticalInsets, right: horizontalInsets)
super.prepare()
}
}
所以我很确定在我的代码中有一些错误,但是找不到它们((任何建议都会对我有帮助!
更新:所以首先感谢! 我已经改变了我的代码和替换一个真正大的图像与最小的一个,和函数得到主色简单的白色目前,事情已经变得更好!
但是,当第一个滚动开始时,只有第一个和第二个单元格滚动到屏幕的左边缘时,才会有很小的滞后或延迟。 之后,所有单元格都在两个方向(左/右)上滚动,甚至是前两个单元格。
现在我的代码如下所示:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "BookCoverCell", for: indexPath) as! BookCoverCell
let book = books![indexPath.row]
let cover = UIImage(named: "flag.png")
let color = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
cell.view1.backgroundColor = color
cell.imageView.image = cover
cell.pageLabel.text = "Pages: 29"
cell.readButton.setTitle("Read", for: .normal)
return cell
}
和更新#2删除
flowLayout = ZoomAndSnapFlowLayout()
collectionView.collectionViewLayout = flowLayout
到viewWillAppease()
也修复这些小滞后!! 万岁!)
阅读这篇关于时间分析和集合视图的文章:https://voxels.github.io/enterming-collection-view-tearing-with-xcode-time-profiler-instrument
由于您的实现非常简单,所以不会有很多错误,但是您可能会遇到与本文作者相同的问题--图像本身非常大,需要读取并调整大小。
他们通过制作适当大小的图像版本解决了这个问题。
在您的情况下,GetDominantColor
在大图像上也会慢一些(我假设它读取像素以获得主色。您还应该考虑缓存此颜色,并且不要每次都重新计算它(如果您还没有这样做的话)。