在分页的“收藏夹视图”中滚动滞后

时间:2020-06-10 13:29:23

标签: ios swift uicollectionview uicollectionviewcell uicollectionviewlayout

我创建了一个集合视图,该视图在每个屏幕上都具有一个启用了分页的单元格。它始终具有4个静态单元。什么都不会从服务器中提取,只有一个请求在后台线程中发送到Firebase。想法是在用户跳入应用程序之前显示一些静态问题。

我的问题是,当您第一次滚动到下一个单元格时,动画不稳定且不像第二次那样平滑,请检查下面的gif或下载屏幕录像:

Link to Screen recording

Gif of lag

如何从一开始就优化性能并获得流畅的动画?

这是我的代码:

集合视图方法:

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {

    return 0
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

    let frameSize = collectionView.frame.size
    return CGSize(width: frameSize.width, height: frameSize.height)
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {

    return UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
}

func numberOfSections(in collectionView: UICollectionView) -> Int {

    return 1
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

    return numberOfSetupItems.count
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    UserMainData.shared.setupItem = indexPath.item - 1
    if !(UserMainData.shared.setupItem == 0) && !(UserMainData.shared.setupItem == -1)  {
        FirebaseManager.shared.updateAllUserData(user: UserMainData.shared) { _ in }
    }
    switch numberOfSetupItems[indexPath.item] {
    case 0:
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "WelcomeCollectionViewCell", for: indexPath)
        guard let welcomeCell = cell as? WelcomeCollectionViewCell else { return cell }

        welcomeCell.didPressNext = { _ in
            DispatchQueue.main.asyncAfter(deadline: .now() + 0.25, execute: {
                collectionView.scrollToItem(at: [0, 1], at: .centeredHorizontally, animated: true)
            })
        }

        return welcomeCell
    case 1:
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "FirstQuestionCollectionViewCell", for: indexPath)
        guard let firstQuestionCell = cell as? FirstQuestionCollectionViewCell else { return cell }

        firstQuestionCell.didSelectOption = { _ in
            DispatchQueue.main.asyncAfter(deadline: .now() + 0.25, execute: {
                collectionView.scrollToItem(at: [0, 2], at: .centeredHorizontally, animated: true)
            })
        }

        return firstQuestionCell
    case 2:
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "SecondQuestionCollectionViewCell", for: indexPath)
        guard let secondQuestionCell = cell as? SecondQuestionCollectionViewCell else { return cell }

        secondQuestionCell.didSelectSave = { _ in
            DispatchQueue.main.asyncAfter(deadline: .now() + 0.25, execute: {
                collectionView.scrollToItem(at: [0, 3], at: .centeredHorizontally, animated: true)
            })
        }
        secondQuestionCell.didSelectCancel = { _ in
            self.output?.openLocationPopup()
            DispatchQueue.main.asyncAfter(deadline: .now() + 0.15, execute: {
                collectionView.scrollToItem(at: [0, 3], at: .centeredHorizontally, animated: false)
            })
        }

        return secondQuestionCell
    case 3:
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "FinalCollectionViewCell", for: indexPath)
        guard let finalQuestionCell = cell as? FinalCollectionViewCell else { return cell }

        finalQuestionCell.didSelectTrack = { _ in
            self.output?.openMainPages(pageNumber: 1)
        }

        return finalQuestionCell
    default:
        return UICollectionViewCell()
    }
}

这是我设置分页的方法:

    func setupCollectionView() {
    let layout = UICollectionViewFlowLayout()
    layout.minimumInteritemSpacing = 0.0
    layout.minimumLineSpacing = 0.0
    layout.itemSize = setupListCollectionView.bounds.size
    layout.scrollDirection = UICollectionView.ScrollDirection.horizontal
    layout.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)

    setupListCollectionView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
    setupListCollectionView.collectionViewLayout = layout
    setupListCollectionView.isPagingEnabled = true

    setupListCollectionView.register(UINib(nibName: "WelcomeCollectionViewCell", bundle: nil),
                                     forCellWithReuseIdentifier: "WelcomeCollectionViewCell")
    setupListCollectionView.register(UINib(nibName: "FirstQuestionCollectionViewCell", bundle: nil),
                                     forCellWithReuseIdentifier: "FirstQuestionCollectionViewCell")
    setupListCollectionView.register(UINib(nibName: "SecondQuestionCollectionViewCell", bundle: nil),
                                     forCellWithReuseIdentifier: "SecondQuestionCollectionViewCell")
    setupListCollectionView.register(UINib(nibName: "FinalCollectionViewCell", bundle: nil),
                                     forCellWithReuseIdentifier: "FinalCollectionViewCell")
}

此方法在viewDidLoad

中调用

0 个答案:

没有答案