我正在尝试构建一个Instagram的克隆,我决定使用一个返回3个不同单元格的UICollectionViewController,并将滚动方向设置为水平,并将分页设置为true,这样我就可以有三个不同的页面2个垂直单元格我将加载2个其他uicollectionviewcell,它们嵌套1用于dm的feed 1,当用户到达相机时隐藏导航栏我遇到了问题因为Instagram具有为Feed单元格和消息单元格显示的导航栏,但不为相机单元格显示导航栏。下面是我的maincollectionviewcontroller的代码。
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 :(得分: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
}
}