0

Instagram의 복제본을 만들려고합니다. 3 개의 다른 셀을 반환하는 UICollectionViewController를 사용하기로 결정하고 가로 방향으로 스크롤 방향을 설정하고 페이지 매김을 true로 설정하고, 그래서 내가 2 개의 수직 셀에 3 개의 서로 다른 페이지를 가질 수있다. 2 개의 다른 uicollectionviewcell은 중첩되어있다. 1은 dm을 위해, 1은 사용자가 도착했을 때 탐색 바를 숨길 때 문제가된다. 인스 타 그램에는 피드 셀과 메시징 셀에 대해 탐색 줄이 표시되지만 카메라 셀에는 표시되지 않기 때문에 카메라로 전송할 수 있습니다. 아래는 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

내의 UIViewController 때문에, UIScrollViewDelegate을 추가했다. 당신이 제공 한 것을 바탕으로 해석적인 대답이 있습니다. 한 번에 하나의 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 
    } 
}