iOS-自定义过渡不显示新的视图控制器

时间:2019-06-07 13:25:00

标签: ios swift

我已编写逻辑来在两个vc之间创建自定义过渡,即从HomeVCHabitListVCHomeVC的底部包含一个集合视图,可进行水平滚动。自定义过渡类似于SnapChat处理它以呈现和消除故事的方式。

我面临的问题是,当显示HabitVC时,我看不到它包含的任何视图。 HabitVC包含一个集合视图,其中每个单元格都是全屏(vc.view的全宽和全高)。

这是与此问题相关的代码

PopAnimator.swift

extension PopAnimator: UIViewControllerAnimatedTransitioning {
    func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
        return TimeInterval(self.transitionDuration)
    }

    func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
        let containerView = transitionContext.containerView

        if self.transitionMode == .Present {
            guard let presentedViewController = transitionContext.viewController(forKey: .to) as? HabitListViewController else { return }
            guard let presentedView = presentedViewController.view else { return }

            let presentedViewCenter = presentedView.center
            let presentedViewSize = presentedView.frame.size

            circleView.frame = frameForCircle(withPresentedViewCenter: presentedViewCenter, size: presentedViewSize, startPoint: self.startingPoint)
            circleView.layer.cornerRadius = circleView.frame.size.width / 2
            circleView.center = startingPoint
            circleView.backgroundColor = .purple
            circleView.transform = CGAffineTransform(scaleX: 0.001, y: 0.001)
            containerView.addSubview(circleView)

            presentedView.center = startingPoint
            presentedView.transform = CGAffineTransform(scaleX: 0.001, y: 0.001)
            presentedView.alpha = 0
            containerView.addSubview(presentedView)

            UIView.animate(withDuration: self.transitionDuration, animations: {
                self.circleView.transform = CGAffineTransform.identity
                presentedView.center = presentedViewCenter
                presentedView.alpha = 1.0
                presentedView.backgroundColor = .red
                containerView.sendSubviewToBack(self.circleView)
            }) { (finished) in
                transitionContext.completeTransition(true)
            }
        }
    }

    private func frameForCircle (withPresentedViewCenter withCenter: CGPoint, size viewSize: CGSize, startPoint: CGPoint) -> CGRect {
        let x = fmax(startPoint.x, viewSize.width - startPoint.x)
        let y = fmax(startPoint.y, viewSize.height - startPoint.y)

        let offsetVector = sqrt(x * x + y * y) * 2
        let size = CGSize(width: offsetVector, height: offsetVector)

        return CGRect(origin: .zero, size: size)
    }
}

我在此处添加一个紫色视图,该视图以圆圈开始,然后扩展到VC。

这就是调用它的方式

extension HomeViewController: UIViewControllerTransitioningDelegate {
    func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        return PopAnimator(transitionDuration: 0.25, transitionMode: .Present, startingPoint: self.startingPoint)
    }
}

HabitVC上没有显示此代码,但是如果我打印vc.view.subviews,则它具有收藏夹视图

class HabitListViewController: UIViewController {

    var collection_view: UICollectionView?
    let habit_cell_idenfifier = "habit_cell"

    override func viewDidLoad() {
        super.viewDidLoad()
        self.setup()
        print(self.view.subviews)
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
    }

    fileprivate func setup() {
        let layout = UICollectionViewFlowLayout()
        layout.scrollDirection = .horizontal
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0

        self.collection_view = UICollectionView(frame: .zero, collectionViewLayout: layout)
        guard let unwrapped_collection_view = self.collection_view else { return }

        self.view.addSubview(unwrapped_collection_view)

        let habit_collection_view_cell = UINib(nibName: "HabitCollectionViewCell", bundle: nil)
        unwrapped_collection_view.register(habit_collection_view_cell, forCellWithReuseIdentifier: self.habit_cell_idenfifier)
        unwrapped_collection_view.backgroundColor = UIColor.red

        unwrapped_collection_view.translatesAutoresizingMaskIntoConstraints = false
        unwrapped_collection_view.leadingAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.leadingAnchor).isActive = true
        unwrapped_collection_view.trailingAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.trailingAnchor).isActive = true
        unwrapped_collection_view.topAnchor.constraint(equalTo: self.view.topAnchor).isActive = true
        unwrapped_collection_view.bottomAnchor.constraint(equalTo: self.view.bottomAnchor).isActive = true
        unwrapped_collection_view.contentInsetAdjustmentBehavior = .never
        unwrapped_collection_view.isPagingEnabled = true
        unwrapped_collection_view.delegate = self
        unwrapped_collection_view.dataSource = self
    }

}

我想念什么?

0 个答案:

没有答案