我有一个UICollectionView
,有20个细胞和一个部分。我制作了每个单元320宽304高。
我使用scrollToItemAtIndexPath(currentIndex + 1)
在集合视图底部使用两个按钮以编程方式滚动集合视图。我只滚动它们1乘1。
这在iPhone 4s和iPhone 5/5s中工作得很好。使用iPhone 6/6 Plus时会出现问题。
当我scrollToItemAtIndexPath
时,它一次滚动两个单元格。我怎样才能防止这种情况发生呢?我试图使单元格符合屏幕的宽度,但只出现了一个单元格,其余的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
}
}
发布于 2016-02-07 05:32:09
我已经使用集合视图中的自定义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)
}
}
https://stackoverflow.com/questions/33337100
复制相似问题