2

今天发现了一个奇怪的bug。这是一个GIF。 在此处输入图像描述

并且为了很好地衡量一个图像清楚地显示它。 在此处输入图像描述

如您所见,在推送到下一个视图控制器之前, UICollectionViewin会出现奇怪的断断续续的偏移行为。topViewController

我怀疑问题可能出在自定义演示文稿上,UINavigationController它显示了我从中推送的水平卡片。

这是如何下降的自定义代码。出现故障的视图控制器只是navigationController?.pushViewController(viewController, animated: true)从顶部视图控制器的普通推送。

片段 A - 演示文稿

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    guard let viewModel = viewModel else { return }
    // V This is a UINavigationController
    let viewController = viewModel.getViewController(displayingOfferAt: indexPath.item)
    viewController.modalPresentationStyle = .custom
    viewController.transitioningDelegate = self
    present(viewController, animated: true)
}

片段 B - 过渡代表

extension StorefrontViewController: UIViewControllerTransitioningDelegate {
    func presentationController(forPresented presented: UIViewController, presenting: UIViewController?, source: UIViewController) -> UIPresentationController? {
        switch presented {
        case is CashStore.FullNavigationController:
            return CashStore.FullPresentationController(presentedViewController: presented, presenting: presenting)
        default:
            return nil
        }
    }
}

Snippet C - 演示控制器

class FullPresentationController: UIPresentationController {

    // MARK: Outlets

    private lazy var backgroundView: UIView = {
        $0.isUserInteractionEnabled = true
        $0.backgroundColor = #colorLiteral(red: 0, green: 0, blue: 0, alpha: 0.8016641695)

        return $0
    }(UIView())


    // MARK: Overrides

    override func containerViewDidLayoutSubviews() {
        super.containerViewDidLayoutSubviews()

        presentedView?.frame = frameOfPresentedViewInContainerView
        backgroundView.frame = containerView!.bounds
    }

    override func presentationTransitionWillBegin() {
        backgroundView.alpha = 0.0
        containerView?.addSubview(backgroundView)
        presentedViewController.transitionCoordinator?.animate(alongsideTransition: { context in
            self.backgroundView.alpha = 1.0
        })
    }

    override func dismissalTransitionWillBegin() {
        presentedViewController.transitionCoordinator?.animate(alongsideTransition: { context in
            self.backgroundView.alpha = 0.0
        }, completion: { context in
            self.backgroundView.removeFromSuperview()
        })
    }


    // MARK: Actions

    @objc func dismiss() {
        presentingViewController.dismiss(animated: true)
    }
}
class FullNavigationController: TiseNavigationController {
    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = .clear
    }
}

Snippet D - 用于捕捉的自定义集合视图布局

class ProductFlowLayout: UICollectionViewFlowLayout {

    override init() {
        super.init()
        scrollDirection = .horizontal
    }

    required init?(coder _: NSCoder) {
        return nil
    }

    override func targetContentOffset(forProposedContentOffset proposedContentOffset: CGPoint, withScrollingVelocity velocity: CGPoint) -> CGPoint {
        guard let collectionView = collectionView else {
            return super.targetContentOffset(forProposedContentOffset: proposedContentOffset, withScrollingVelocity: velocity)
        }

        var offsetAdjustment = CGFloat.greatestFiniteMagnitude
        let horizontalOffset = proposedContentOffset.x + collectionView.contentInset.left

        let targetRect = CGRect(x: proposedContentOffset.x, y: 0, width: collectionView.bounds.size.width, height: collectionView.bounds.size.height)

        let layoutAttributesArray = super.layoutAttributesForElements(in: targetRect) ?? []

        for attributes in layoutAttributesArray where fabsf(Float(attributes.frame.origin.x - horizontalOffset)) < fabsf(Float(offsetAdjustment)) {
            offsetAdjustment = attributes.frame.origin.x - horizontalOffset
        }

        let flowDelegate = collectionView.delegate as? UICollectionViewDelegateFlowLayout
        let leftInset = flowDelegate?.collectionView?(collectionView, layout: self, insetForSectionAt: 0).left ?? 0.0
        let targetOffset = CGPoint(x: proposedContentOffset.x + offsetAdjustment - leftInset, y: proposedContentOffset.y)

        return targetOffset
    }
}

我尝试禁用内容偏移调整,这并没有改变任何东西。我还尝试禁用maskToBounds带有卡片的视图控制器和集合视图,但无济于事。抱歉问题太长了,我希望有人能帮我解决这个奇怪的故障!

4

0 回答 0