CollectionView 单元格序列

CollectionView Cell segue

我在 CollectionView 单元格中的 thumbnailImage 上有一个点击手势,当 pushed/presented/etc 到另一个没有情节提要的视图控制器时,它工作了,因为我实现了情节提要,它不再工作了,我尝试了许多不同的解决方案并且没有过渡。我在故事板上设置了 class 但一切都是以编程方式完成的。我想知道是否有任何方法可以从 CollectionView 单元格从当前 ViewController 切换到另一个。

故事板图片:

在模拟器中是这样的:

import UIKit

    class Cell: UICollectionViewCell{

        override init(frame: CGRect) {
            super.init(frame: frame)
            setupViews()

            let tapRecognizer: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(Tap(recognizer:)))
            thumbnailImage.isUserInteractionEnabled = true
            thumbnailImage.addGestureRecognizer(tapRecognizer)

        }

        func setupViews(){
            addSubview(thumbnailImage)
            addSubview(separatorView)
            addSubview(brandName)
            addSubview(Priceing)
            addSubview(model)


            addConstraintsWithFormat(format: "H:|-16-[v0]-16-|", views: thumbnailImage)
            addConstraintsWithFormat(format: "H:|[v0]|", views: separatorView)

            //Vertical Contsraint
            addConstraintsWithFormat(format: "V:[v0(200)]-87-[v1(1)]|", views: thumbnailImage, separatorView)
            addConstraintsWithFormat(format: "V:[v0(20)]", views: brandName)
            addConstraintsWithFormat(format: "V:[v0(20)]", views: Priceing)
            addConstraintsWithFormat(format: "V:[v0(20)]", views: model)

            addConstraint(NSLayoutConstraint(item: brandName, attribute: .top, relatedBy: .equal, toItem: thumbnailImage, attribute: .bottom, multiplier: 1, constant: 8))
            addConstraint(NSLayoutConstraint(item: brandName, attribute: .right, relatedBy: .equal, toItem: thumbnailImage, attribute: .right, multiplier: 1, constant: 0))
            addConstraint(NSLayoutConstraint(item: brandName, attribute: .left, relatedBy: .equal, toItem: thumbnailImage, attribute: .left, multiplier: 1, constant: 0))

            addConstraint(NSLayoutConstraint(item: Priceing, attribute: .top, relatedBy: .equal, toItem: brandName, attribute: .bottom, multiplier: 1, constant: 4))
            addConstraint(NSLayoutConstraint(item: Priceing, attribute: .right, relatedBy: .equal, toItem: brandName, attribute: .right, multiplier: 1, constant: 0))
            addConstraint(NSLayoutConstraint(item: Priceing, attribute: .left, relatedBy: .equal, toItem: brandName, attribute: .left, multiplier: 1, constant: 0))

            addConstraint(NSLayoutConstraint(item: model, attribute: .top, relatedBy: .equal, toItem: Priceing, attribute: .bottom, multiplier: 1, constant: 4))
            addConstraint(NSLayoutConstraint(item: model, attribute: .right, relatedBy: .equal, toItem: Priceing, attribute: .right, multiplier: 1, constant: 0))
            addConstraint(NSLayoutConstraint(item: model, attribute: .left, relatedBy: .equal, toItem: Priceing, attribute: .left, multiplier: 1, constant: 0))
        }

        let separatorView: UIView = {
            let view = UIView()
            view.backgroundColor = UIColor(displayP3Red: 230/255, green: 230/255, blue: 230/255, alpha: 1)
            view.translatesAutoresizingMaskIntoConstraints = false
            return view
        }()

        let brandName: UILabel = {
            let label = UILabel()
            label.translatesAutoresizingMaskIntoConstraints = false
            label.backgroundColor = UIColor.rgb(displayP3Red: 211, green: 211, blue: 211)
            label.numberOfLines = 2
            return label
        }()

        let model: UILabel = {
            let label = UILabel()
            label.translatesAutoresizingMaskIntoConstraints = false
            label.backgroundColor = UIColor.rgb(displayP3Red: 211, green: 211, blue: 211)
            label.numberOfLines = 1
            return label
        }()

        let Priceing: UILabel = {
            let textView = UILabel()
            textView.translatesAutoresizingMaskIntoConstraints = false
            textView.numberOfLines = 1
            textView.backgroundColor = UIColor.rgb(displayP3Red: 211, green: 211, blue: 211)
            return textView
        }()

        let thumbnailImage: UIImageView = {
            let image = UIImageView()
            image.backgroundColor = UIColor.rgb(displayP3Red: 211, green: 211, blue: 211)
            image.contentMode = .scaleAspectFill
            image.clipsToBounds = true
            return image
        }()

        @objc func Tap(recognizer: UITapGestureRecognizer){
            let vc = DescriptionViewController()
    //        let nc = self.window?.rootViewController?.navigationController
    //        let transition = CATransition()
    //        transition.duration = 0.3
    //        transition.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
    //        transition.type = kCATransitionMoveIn
    //        transition.subtype = kCATransitionFromTop
    //        nc?.navigationController?.view.layer.add(transition, forKey: nil)
            self.window?.rootViewController?.navigationController?.pushViewController(vc, animated: false)
        }



        required init?(coder aDecoder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
    }

使用此扩展程序找到您的 UIView parent UIViewController

extension UIView {

    var parentViewController: UIViewController? {
        var parentResponder: UIResponder? = self
        while parentResponder != nil {
            parentResponder = parentResponder!.next
            if let viewController = parentResponder as? UIViewController {
                return viewController
            }
        }
        return nil
    }
}

在您的项目中添加此扩展程序并像那样调用

    if let vc = self.parentViewController as? YourPresentedViewController{
        let pushVc = DescriptionViewController()
        vc.navigationController?.pushViewController(pushVc, animated: true)
    }

注意 :- UITableView 添加到 YourPresentedViewController, 所以我的表格视图 parentViewController 是 YourPresentedViewController.