在过渡期间,自定义呈现的UINavigationController会出现UICollectionView的contentOffset故障

时间:2018-11-02 14:57:59

标签: ios swift uinavigationcontroller uikit custom-transition

我今天发现了一个奇怪的错误。这是一个gif。 enter image description here

并最好将影像清晰地显示出来。 enter image description here

如您所见,UICollectionView中的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
        }
    }
}

代码段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
    }
}

代码段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,但无济于事。对这个冗长的问题感到抱歉,我希望有人可以帮助我解决这个怪异的问题!

0 个答案:

没有答案