gpt4 book ai didi

ios - 填充整个屏幕宽度并且一次仅滚动一个的集合单元格?

转载 作者:行者123 更新时间:2023-11-30 13:58:11 25 4
gpt4 key购买 nike

我有一个 UICollectionView,它有 20 个单元格和 1 个部分。我将每个单元格的宽度设为 320,高度设为 304。

我使用 scrollToItemAtIndexPath(currentIndex + 1) 使用 Collection View 底部的两个按钮以编程方式滚动 Collection View 。我只将它们一一滚动。

这在 iPhone 4s 和 iPhone 5/5s 上运行良好。使用 iPhone 6/6 Plus 时会出现此问题。

当我scrollToItemAtIndexPath时,它一次滚动2个单元格。我怎样才能防止这种情况发生?我尝试使单元格适合屏幕宽度,但只出现一个单元格,UICollectionView 的其余部分是黑色的。

编辑:

这是数据源代码:

extension ViewController: UICollectionViewDataSource {

func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 1
}

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 32
}

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("collectionCell", forIndexPath: indexPath) as! CollectionCell
cell.backgroundColor = UIColor.whiteColor()

self.current = indexPath

self.configureCell(cell, atIndexPath: indexPath) as! CollectionCell
return cell
}

func configureCell(cell: CollectionCell, atIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let string = textArray[indexPath.item % 20] as String
cell.textView.text = string
cell.textView.backgroundColor = UIColor.cloudsColor()
return cell
}
}

这是我滚动它们的方式:

func buttonSelected(sender: UIButton) {
switch sender.tag {
case 0:
let previousItem: NSIndexPath = NSIndexPath(forItem: self.current.item - 1, inSection: self.current.section)
self.collectionView?.scrollToItemAtIndexPath(previousItem, atScrollPosition:UICollectionViewScrollPosition.CenteredHorizontally, animated:true)
case 1:
let nextItem: NSIndexPath = NSIndexPath(forItem: self.current.item + 1, inSection: self.current.section)
self.collectionView?.scrollToItemAtIndexPath(nextItem, atScrollPosition:UICollectionViewScrollPosition.CenteredHorizontally, animated:true)
default: break
}
}

最佳答案

我已经在 Collection View 中使用自定义 FlowLayout 解决了这个问题。这是:

    class CenterFlowLayout: UICollectionViewFlowLayout {

override func targetContentOffsetForProposedContentOffset(proposedContentOffset: CGPoint, withScrollingVelocity velocity: CGPoint) -> CGPoint {
if let cv = self.collectionView {
let cvBounds = cv.bounds
let halfWidth = cvBounds.size.width * 0.5
let proposedContentOffsetCenterX = proposedContentOffset.x + halfWidth
if let attributesForVisibleCells = self.layoutAttributesForElementsInRect(cvBounds) as [UICollectionViewLayoutAttributes]! {
var candidateAttributes: UICollectionViewLayoutAttributes?
for attributes in attributesForVisibleCells {
// == Skip comparison with non-cell items (headers and footers) == //
if attributes.representedElementCategory != UICollectionElementCategory.Cell {
continue
}
if let candAttrs = candidateAttributes {
let a = attributes.center.x - proposedContentOffsetCenterX
let b = candAttrs.center.x - proposedContentOffsetCenterX
if fabsf(Float(a)) < fabsf(Float(b)) {
candidateAttributes = attributes
}
} else { // == First time in the loop == //
candidateAttributes = attributes
continue
}
}
return CGPoint(x : candidateAttributes!.center.x - halfWidth, y : proposedContentOffset.y)
}
}
// Fallback
return super.targetContentOffsetForProposedContentOffset(proposedContentOffset)
}
}

关于ios - 填充整个屏幕宽度并且一次仅滚动一个的集合单元格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33337100/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com