LargeTitle,CollectionView和UIRefreshControl跳转/闪烁故障

问题描述

在我的一个项目中,我注意到使用UIRefreshControl和大标题时有些奇怪的行为。 更具体地说,我是在谈论怪异的毛刺(我认为这是在重新计算布局时发生的)。 为了说明这个包,我创建了一个简单的项目,该项目由一个ViewController和一个NavigationController(启用了大标题),UICollectionView和UIRefreshControl组成。 由于设置了“数据”变量(首次启动时为10),因此设置了Collectionview,并且每次触发刷新控制时,数据变量都会以+ = 1的方式对其计数进行计数(didSet属性正在重新加载集合视图结束触发器endRefreshing )。您可以在下面查看源代码以及问题本身。

GLITCH GIF

import UIKit

class ViewController: UIViewController {

@IBOutlet var collectionView: UICollectionView!

var data = 2 {
    didSet {
        collectionView.reloadData()
        dispatchQueue.main.async {
            self.collectionView.refreshControl?.endRefreshing()
        }
    }
}

override func viewDidLoad() {
    super.viewDidLoad()
    setupCollectionView()
    setupRefreshControl()
    navigationController?.navigationBar.prefersLargeTitles = true
}

func setupCollectionView() {
    collectionView.delegate = self
    let nib = UINib(nibName: "CollectionViewCell",bundle: nil)
    collectionView.register(nib,forCellWithReuseIdentifier: "CollectionViewCell")
}

func setupRefreshControl() {
    let refreshControl = UIRefreshControl()
    refreshControl.addTarget(self,action: #selector(didStartRefreshing),for: .valueChanged)
    collectionView.refreshControl = refreshControl
}

@objc func didStartRefreshing() {
//        dispatchQueue.main.asyncAfter(deadline: .Now() + 1.0) { [weak self] in
//            guard let self = self else { return }
        self.data += 1
//        }
  }
}

extension ViewController: UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView,numberOfItemsInSection section: Int) -> Int {
    data
}

func collectionView(_ collectionView: UICollectionView,cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionViewCell",for: indexPath) as! CollectionViewCell
    return cell
}


}

extension ViewController: UICollectionViewDelegateFlowLayout {

func collectionView(_ collectionView: UICollectionView,layout collectionViewLayout: UICollectionViewLayout,insetForSectionAt section: Int) -> UIEdgeInsets {
    return UIEdgeInsets(top: 10,left: 10,bottom: 100,right: 10)
}

func collectionView(_ collectionView: UICollectionView,sizeforItemAt indexPath: IndexPath) -> CGSize {
    return CGSize(width: UIScreen.main.bounds.size.width/2-15,height: 225)
}
func collectionView(_ collectionView: UICollectionView,minimumLinespacingForSectionAt section: Int) -> CGFloat {
    return 10
}
func collectionView(_ collectionView: UICollectionView,didSelectItemAt indexPath: IndexPath) {
}
func collectionView(_ collectionView: UICollectionView,diddeselectItemAt indexPath: IndexPath) {
}
}

检查堆栈溢出,我没有发现任何东西,但是集合视图的约束设置错误的情况并非如此(集合视图的每个约束都链接到超级视图) 我遇到的唯一决定是在第一时间触发RefreshControl时在1秒之前使用asyncAfter,但这似乎是一个非常糟糕的决定(在上面的代码中,我注释了以下几行,但如果我取消注释,此故障将不再出现)。 问题是:是否有最佳实践来避免此问题,或者也许有人有更好的决定?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)