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