点击CollectionView Cell的下半部分时推动ViewController

时间:2017-12-26 00:59:51

标签: swift uinavigationcontroller uicollectionview uicollectionviewcell

我有UICollectionView包含其他四个子视图。当我点击ThumbnailImage时它没有被推动,如果我点击底部三个视图它会被推到任何想法为什么?我已经以编程方式完成了这一切,并在Storyboard中设置了类。

Picture of Simulator

Picture of Storyboard

我的手机。

import UIKit

class Cell: UICollectionViewCell{

    var myVC: MainFeedViewController!

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

    }

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

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

我的ViewController。

import UIKit

    class MainFeedViewController: UIViewController, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource, UIViewControllerTransitioningDelegate, UISearchBarDelegate {

        let transition = CircularTransition()
        var collectionView: UICollectionView!
        lazy var searchBar:UISearchBar = UISearchBar()

        override func viewDidLoad() {
            super.viewDidLoad()

            let toolBar = UIToolbar()
            let doneButton = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(doneClicked))
            toolBar.setItems([doneButton], animated: false)
            toolBar.sizeToFit()
            searchBar.inputAccessoryView = toolBar

            view.backgroundColor = .white
            UIApplication.shared.isStatusBarHidden = false
            UIApplication.shared.statusBarStyle = .default
            view.addSubview(Settings)
            view.addSubview(topBar)
            view.addSubview(separatorView)
            view.addSubview(separatorView2)
            view.addSubview(searchBar)

            Settings.translatesAutoresizingMaskIntoConstraints = false
            Settings.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 45).isActive = true
            Settings.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
            Settings.widthAnchor.constraint(equalToConstant: 50).isActive = true
            Settings.heightAnchor.constraint(equalToConstant: 50).isActive = true

            separatorView.translatesAutoresizingMaskIntoConstraints = false
            separatorView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 70).isActive = true
            separatorView.heightAnchor.constraint(equalToConstant: 1).isActive = true
            view.addConstraint(NSLayoutConstraint(item: separatorView, attribute: .left, relatedBy: .equal, toItem: Settings, attribute: .right, multiplier: 1, constant: 15))
            view.addConstraint(NSLayoutConstraint(item: separatorView, attribute: .right, relatedBy: .equal, toItem: topBar, attribute: .right, multiplier: 1, constant: 0))

            separatorView2.translatesAutoresizingMaskIntoConstraints = false
            separatorView2.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 70).isActive = true
            separatorView2.heightAnchor.constraint(equalToConstant: 1).isActive = true
            view.addConstraint(NSLayoutConstraint(item: separatorView2, attribute: .right, relatedBy: .equal, toItem: Settings, attribute: .left, multiplier: 1, constant: -15))
            view.addConstraint(NSLayoutConstraint(item: separatorView2, attribute: .left, relatedBy: .equal, toItem: topBar, attribute: .left, multiplier: 1, constant: 0))

            topBar.translatesAutoresizingMaskIntoConstraints = false
            topBar.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
            topBar.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true
            topBar.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
            view.addConstraint(NSLayoutConstraint(item: topBar, attribute: .bottom, relatedBy: .equal, toItem: separatorView, attribute: .top, multiplier: 1, constant: 0))
            view.insertSubview(topBar, belowSubview: Settings)

            searchBar.searchBarStyle = UISearchBarStyle.prominent
            searchBar.placeholder = "Search"
            searchBar.sizeToFit()
            searchBar.isTranslucent = false
            searchBar.backgroundImage = UIImage()
            searchBar.delegate = self
            searchBar.layer.cornerRadius = 18.5
            searchBar.layer.borderWidth = 2
            searchBar.translatesAutoresizingMaskIntoConstraints = false
            searchBar.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true
            searchBar.heightAnchor.constraint(equalToConstant: 37).isActive = true
            view.addConstraint(NSLayoutConstraint(item: searchBar, attribute: .bottom, relatedBy: .equal, toItem: Settings, attribute: .top, multiplier: 1, constant: -3))

            let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
            let height = (view.frame.width - 16 - 16) * 9/16
            layout.sectionInset = UIEdgeInsets(top: 110, left: 0, bottom: 0, right: 0)
            layout.itemSize = CGSize(width: view.frame.width, height: height + 16 + 80)

            collectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout)
            collectionView?.scrollIndicatorInsets = UIEdgeInsetsMake(110, 0, 0, 0)
            collectionView.dataSource = self
            collectionView.delegate = self
            collectionView.register(Cell.self, forCellWithReuseIdentifier: "cellId")
            collectionView.backgroundColor = UIColor.clear
            self.view.addSubview(collectionView)
            view.insertSubview(collectionView, belowSubview: topBar)

        }

        let Settings : UIButton = {
            let btn = UIButton()
            btn.setTitle("Onnevo", for: .normal)
            btn.setTitleColor(.white, for: .normal)
            btn.layer.cornerRadius = 25
            btn.backgroundColor = UIColor.rgb(displayP3Red: 255, green: 165, blue: 0)
            btn.titleLabel?.font = UIFont(name: "Pacifico-Regular", size: 16)
            btn.addTarget(self, action:#selector(settingsTab), for: .touchUpInside)
            return btn
        }()

        let topBar: UIView = {
            let bar = UIView()
            bar.backgroundColor = .white

            return bar
        }()

        let separatorView: UIView = {
            let view = UIView()
            view.backgroundColor = UIColor.rgb(displayP3Red: 211, green: 211, blue: 211)
            return view
        }()

        let separatorView2: UIView = {
            let view2 = UIView()
            view2.backgroundColor = UIColor.rgb(displayP3Red: 211, green: 211, blue: 211)
            return view2
        }()

        func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {
            print(123)
        }

        func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
            return 4
        }

        func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellId", for: indexPath) as! Cell
            cell.myVC = self
            return cell
        }

        func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
            let cell: Cell = collectionView.cellForItem(at: indexPath) as! Cell
            let vc = DescriptionViewController()
            cell.myVC.navigationController?.pushViewController(vc, animated: true)
        }

        func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
            transition.transitionMode = .present
            transition.startingPoint = Settings.center
            transition.circleColor = Settings.backgroundColor!

            return transition
        }

        func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
            transition.transitionMode = .dismiss
            transition.startingPoint = Settings.center
            transition.circleColor = Settings.backgroundColor!

            return transition
        }

        @objc func settingsTab(){
            let secondVC = SettingsViewController()
            secondVC.transitioningDelegate = self
            secondVC.modalPresentationStyle = UIModalPresentationStyle.custom
            self.present(secondVC, animated: true, completion: nil)
        }

        @objc func doneClicked(){
            view.endEditing(true )
        }
    }

1 个答案:

答案 0 :(得分:1)

设置缩略图集时,isUserInterationEnabled = false,如

  thumbnailImage.isUserInteractionEnabled = false