微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决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 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?