CollectionView Cell segue

时间:2017-12-25 05:49:43

标签: swift uicollectionview segue uicollectionviewcell

我在CollectionView单元格中的thumbnailImage上有一个轻击手势,当推送/呈现/等到另一个没有故事板的View Controller时,它现在工作了,因为我实现了它不再工作的故事板,我尝试了很多不同的解决方案而且没有过渡。我在故事板上设置了类,但一切都是以编程方式完成的。我想知道是否有任何方法可以从CollectionView单元格切换到当前的ViewController。

故事板的图片:

enter image description here

这就是模拟器中的样子:

enter image description here

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")
        }
    }

1 个答案:

答案 0 :(得分:0)

使用此扩展程序

找到您的UIViewUIViewController
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,因此我的tableview parentViewController是YourPresentedViewController。