代码之家  ›  专栏  ›  技术社区  ›  TheRedCamaro3.0 3.0

当点击按钮时,如何使CollectionView标题变为隐藏?

  •  0
  • TheRedCamaro3.0 3.0  · 技术社区  · 6 年前

    我正在尝试使CollectionView头在点击按钮时变为隐藏,但似乎无法从 IBAction 功能。

    按钮功能

        var buttonPressed:Bool = true
        @IBAction func changeView(_ sender: Any) {
            if buttonPressed{
            UIView.animate(withDuration: 0.05){
    
                self.buttonPressed = !self.buttonPressed
                print("Ive been expanded")
            }
            }
            else{
                UIView.animate(withDuration: 0.05){
                    self.buttonPressed = !self.buttonPressed
                }
            }
        }   
    }
    

    完整的代码

    import UIKit
    
    class ViewController: UIViewController,UICollectionViewDataSource,UICollectionViewDelegate {
    
        @IBOutlet weak var animateCollectionView: UICollectionView!
    
    
    
        override func viewDidLoad() {
            super.viewDidLoad()
            // Do any additional setup after loading the view, typically from a nib.
            animateCollectionView.dataSource = self
            animateCollectionView.delegate = self
            animateCollectionView.backgroundColor = UIColor.blue
            animateCollectionView.frame = CGRect(x: 0, y: 100, width: 600, height: 1000)
            let layout = animateCollectionView.collectionViewLayout as? UICollectionViewFlowLayout
            layout?.sectionHeadersPinToVisibleBounds = true
             self.view.sendSubview(toBack: self.animateCollectionView)
        }
    
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }
    
    
        func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    
            return 200
        }
    
        func numberOfSections(in collectionView: UICollectionView) -> Int {
            return 1
        }
    
        func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
            let cell = self.animateCollectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! customCell
            cell.cellLabel.text = "\(indexPath.item)"
            cell.backgroundColor = UIColor.orange
            return cell
    
    
        }
    
        func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
            // returning the search bar for header
            let header = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "headerCell", for: indexPath) as! customHeader
    
            header.backgroundColor = UIColor.purple
    
            return header
        }
    
        func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
            // if section is above search bar we need to make its height 0
            if section == 0 {
                return CGSize(width: 0, height: 0)
            }
            // for section header i.e. actual search bar
            return CGSize(width: animateCollectionView.frame.width, height: 50)
        }
    
    
        var buttonPressed:Bool = true
        @IBAction func changeView(_ sender: Any) {
            if buttonPressed{
            UIView.animate(withDuration: 0.05){
          self.buttonPressed = !self.buttonPressed
                print("Ive been expanded")
            }
            }
            else{
                UIView.animate(withDuration: 0.05){
    
                }
            }
        }
    
    
    
    
    }
    
    class customCell :UICollectionViewCell{
        @IBOutlet weak var cellLabel: UILabel!
    }
    
    class customHeader: UICollectionReusableView{
    
    }
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   David Henry    6 年前

    protocol HideHeaderViewDelegate {
        func hideHeaderView(hide: Bool)
    }
    
    class HeaderView: UICollectionReusableView {
    
        var delegate: HideHeaderViewDelegate?
    
        @IBOutlet weak var hideButton: UIButton!
    
        override func awakeFromNib() {
            super.awakeFromNib()
        }
    
        @IBAction func hideButtonAction(_ sender: Any) {
            self.frame.size = CGSize.zero
            guard let delegate = delegate else { return }
            delegate.hideHeaderView(hide: true)
        }
    }
    

    extension ViewController: HideHeaderView {
        func hideHeaderView(hide: Bool) {
            if hide == true {
                print(hide)
                // invalidate your layout here
            }
        }
    }
    

    func collectionView(_ collectionView: UICollectionView,
                            viewForSupplementaryElementOfKind kind: String,
                            at indexPath: IndexPath) -> UICollectionReusableView {
            let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionHeader,
                                                                             withReuseIdentifier: "headerView", for: indexPath) as! HeaderView
            headerView.delegate = self
    
        return headerView
        }