0

我想構建一個Instagram的克隆,並且我決定使用返回3個不同單元格的UICollectionViewController,並將滾動方向設置爲水平,並將分頁設置爲true,所以我可以在2個垂直單元中有三個不同的頁面我將加載2個其他的uicollectionviewcell,它們爲dm的feed 1嵌套1,我遇到了一個問題,當用戶獲取時隱藏導航欄因爲Instagram的導航欄顯示的飼料細胞和消息傳遞細胞,但不是相機之一。下面是我的maincollectionviewcontroller代碼。如何呈現UICollectionviewcell的垂直不同的單元格大小

import UIKit 
import AVFoundation 

class MainViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout { 

    let cellID = "cellId" 
    let messageCellID = "messageCellID" 
    let cameraCellID = "cameraCellID" 
    var swipeRight = UISwipeGestureRecognizer() 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     let titleImage = UIImageView(image: #imageLiteral(resourceName: "Instagram_logo")) 
     titleImage.layer.masksToBounds = true 
     self.navigationItem.titleView = titleImage 

     setupCollectionView() 
     setupSwipeGesture() 
     } 

// //Swipe right to get camera 
// func setupSwipeGesture() { 
//  swipeRight.direction = .right 
//  self.navigationController?.isNavigationBarHidden = true 
//  let cameraViewController = ViewController() 
//  cameraViewController.transitioningDelegate = self 
//  navigationController?.pushViewController(cameraViewController, animated: true) 
// } 

    func setupSwipeGesture() { 
     print("trying to swipe") 
     swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(swiped)) 
     self.view.addGestureRecognizer(swipeRight) 
     swipeRight.direction = .right 
    } 

    func swiped(){ 
     print("swipping to get Camera") 
     self.navigationController?.isNavigationBarHidden = true 
     let cameraViewController = ViewController() 
     cameraViewController.transitioningDelegate = self 
     navigationController?.pushViewController(cameraViewController, animated: true) 

    } 


    func setupCollectionView(){ 

     collectionView?.backgroundColor = .white 
     collectionView?.register(MainViewFeedCellCollectionViewCell.self, forCellWithReuseIdentifier: cellID) 
     collectionView?.register(MainViewMessagedFeedCell.self, forCellWithReuseIdentifier: messageCellID) 
     collectionView?.register(MainViewCameraFeed.self, forCellWithReuseIdentifier: cameraCellID) 
     collectionView?.isPagingEnabled = true 
    } 

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

     scrollToMenuIndex(menuIndex: 0) 
    } 

    func goBackToMainPage(){ 
     scrollToMenuIndex(menuIndex: 0) 
    } 

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

    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 

     if (indexPath.item == 2){ 
      return collectionView.dequeueReusableCell(withReuseIdentifier: messageCellID, for: indexPath) 
     } 

     else if (indexPath.item == 0){ 
      let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cameraCellID, for: indexPath) 
      return cell 
     } 

     else if (indexPath.item == 1){ 
      let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellID, for: indexPath) 
      return cell 
     } 

     else{ 
      let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellID, for: indexPath) 
      return cell 
     } 
    } 

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { 
     print("\(indexPath.row)") 
//  if indexPath.row == 0{ 
//   navigationController?.isNavigationBarHidden = true 
//   return CGSize(width: view.frame.width, height:`  view.frame.height) 
     //} 
     return CGSize(width: view.frame.width, height: view.frame.height - 70) 
    } 

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat { 
     return 0 
    } 

    func scrollToMenuIndex(menuIndex: Int){ 
     let index = IndexPath(item: menuIndex, section: 0) 
     collectionView?.scrollToItem(at: index, at: .centeredHorizontally, animated: true) 

    } 

} 

回答

0

我所做的解決類似問題的方法是將UIScrollViewDelegate添加到我的UIViewController。這是基於您提供的解釋性答案。由於每次只出現一個UICollectionViewCell,所以應該這樣做。我不確定這是否是最好的方法來完成它,但它對我有用:

var visibleCurrentCell: IndexPath? { 
    for cell in self.collectionView.visibleCells { 
     let indexPath = self.collectionView.indexPath(for: cell) 
     return indexPath 
    } 

    return nil 
} 

func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) { 
    Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(checkIfCameraIsShowing), userInfo: nil, repeats: false) 
} 

func checkIfCameraIsShowing() { 
    if let visibleCurrCell = visibleCurrentCell, let _ = collectionView.cellForItem(at: visibleCurrCell) as? CameraCollectionViewCell { 
     self.navigationController?.isNavigationBarHidden = true 
    } 
} 
相關問題