【发布时间】:2019-07-11 08:24:26
【问题描述】:
首先,不确定标题是否正确或提供了最佳描述,但我不确定还能使用什么。
所以,我正在开发一个应用程序,但我遇到了在实现 UI 时卡住的部分。 基本上,我有一个 VC(下图),它可以根据我从 JSON 文件中获得的信息进行自我定位。
问题是我需要在上方有一个类似轮播的菜单,其中包含未定义数量的单元格(同样,取决于我从 JSON 文件中获得的内容)。 我决定为此使用 UICollectionView,并且成功地实现了基础知识。
但这是我卡住的部分:
- 由于选定的单元格必须始终居中,因此当第一个和最后一个单元格被选中时,我需要在单元格和安全区域之间留出一个空白区域(参见上图)。
- 需要分页滚动。通常,如果 UICollectionView 单元格的宽度几乎等于屏幕之一,但要求能够一次滚动一个元素(参见上面的第二个屏幕),这不会成为问题。
我试图找到类似的东西,但也许我没有找到正确的东西,因为我只能找到Paging UICollectionView by cells, not screen
另外,老实说,我从未见过有这种行为的应用程序/UICollectionView。
我在下面发布了部分代码,但它并没有太大帮助,因为它只是标准的 UICollectionView 方法。
有什么建议吗?
class PreSignupDataVC : UIViewController, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource, UIPickerViewDelegate, UIPickerViewDataSource
@IBOutlet weak var cvQuestions: UICollectionView!
var questionCell : PreSignupDataQuestionCellVC!
var screenData : Array<PreSignupScreenData> = Array<PreSignupScreenData>()
var pvDataSource : [String] = []
var numberOfComponents : Int = 0
var numberOfRowsInComponent : Int = 0
var currentScreen : Int = 1
var selectedType : Int?
var selectedCell : Int = 0
var initialLastCellInsetPoint : CGFloat = 0.0
override func viewDidLoad()
{
super.viewDidLoad()
print("PreSignupDataVC > viewDidLoad")
initialLastCellInsetPoint = (self.view.frame.width - 170)/2
screenData = DataSingleton.sharedInstance.returnPreSignUpUIArray()[selectedType!].screenData
numberOfComponents = screenData[currentScreen - 1].controls[0].numberOfComponents!
numberOfRowsInComponent = screenData[currentScreen - 1].controls[0].controlDataSource.count
pvDataSource = screenData[currentScreen - 1].controls[0].controlDataSource
cvQuestions.register(UINib(nibName: "PreSignupDataQuestionCell",
bundle: nil),
forCellWithReuseIdentifier: "PreSignupDataQuestionCellVC")
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
{
print("PreSignupDataVC > collectionView > numberOfItemsInSection")
return screenData[currentScreen - 1].controls.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
{
print("PreSignupDataVC > collectionView > cellForItemAt")
questionCell = (cvQuestions.dequeueReusableCell(withReuseIdentifier: "PreSignupDataQuestionCellVC",
for: indexPath) as? PreSignupDataQuestionCellVC)!
questionCell.vQuestionCellCellContainer.layer.cornerRadius = 8.0
questionCell.lblQuestion.text = screenData[currentScreen - 1].controls[indexPath.row].cellTitle
questionCell.ivQuestionCellImage.image = UIImage(named: screenData[currentScreen - 1].controls[indexPath.row].cellUnselectedIcon!)
return questionCell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)
{
print("PreSignupDataVC > collectionView > didSelectItemAt")
numberOfComponents = screenData[currentScreen - 1].controls[indexPath.row].numberOfComponents!
numberOfRowsInComponent = screenData[currentScreen - 1].controls[indexPath.row].controlDataSource.count
pvDataSource = screenData[currentScreen - 1].controls[indexPath.row].controlDataSource
selectedCell = indexPath.row
pvData.reloadAllComponents()
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets
{
print("PreSignupDataVC > collectionView > insetForSectionAt")
return UIEdgeInsets(top: 0.0, left: initialLastCellInsetPoint, bottom: 00.0, right: initialLastCellInsetPoint)
}
【问题讨论】:
-
对于即时解决方案,您可以使用 iCarousel 动画。您可以从 Google 获取。
标签: swift uicollectionview uicollectionviewcell uipickerview scroll-paging