gpt4 book ai didi

ios - 为什么collectionView单元格居中只能在一个方向起作用?

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

我正在尝试将我的单元格在水平滚动上居中。我编写了一种方法,但仅当我向右滚动时它才有效,向左滚动时它只会滚动,而不会在单元格的中心停止。

有人可以帮我定义这个错误吗?

class CenterCellCollectionViewFlowLayout: 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) {
var candidateAttributes: UICollectionViewLayoutAttributes?
for attributes in attributesForVisibleCells {

// == Skip comparison with non-cell items (headers and footers) == //
if attributes.representedElementCategory != UICollectionElementCategory.Cell {
continue
}

if (attributes.center.x == 0) || (attributes.center.x > (cv.contentOffset.x + halfWidth) && velocity.x < 0) {
continue
}

// == First time in the loop == //
guard let candAttrs = candidateAttributes else {
candidateAttributes = attributes
continue
}

let a = attributes.center.x - proposedContentOffsetCenterX
let b = candAttrs.center.x - proposedContentOffsetCenterX

if fabsf(Float(a)) < fabsf(Float(b)) {
candidateAttributes = attributes;
}
}

if(proposedContentOffset.x == -(cv.contentInset.left)) {
return proposedContentOffset
}

return CGPoint(x: floor(candidateAttributes!.center.x - halfWidth), y: proposedContentOffset.y)
}
} else {
print("else")
}

// fallback
return super.targetContentOffsetForProposedContentOffset(proposedContentOffset)
}
}

在我的 UIViewController 中:

override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()

var insets = self.collectionView.contentInset
let value = ((self.view.frame.size.width - ((CGRectGetWidth(collectionView.frame) - 35))) * 0.5)
insets.left = value
insets.right = value
self.collectionView.contentInset = insets

self.collectionView.decelerationRate = UIScrollViewDecelerationRateNormal
}

如果您有任何问题,请问我

最佳答案

实际上,我只是对另一个线程做了稍微不同的实现,但对其进行了调整以解决这个问题。尝试下面的解决方案:)

/**
* Custom FlowLayout
* Tracks the currently visible index and updates the proposed content offset
*/
class CustomCollectionViewFlowLayout: UICollectionViewFlowLayout {

// Tracks the currently visible index
private var visibleIndex : Int = 0

// The width offset threshold percentage from 0 - 1
let thresholdOffsetPrecentage : CGFloat = 0.5

// This is the flick velocity threshold
let velocityThreshold : CGFloat = 0.4

override init() {
super.init()
self.minimumInteritemSpacing = 0.0
self.minimumLineSpacing = 0.0
self.scrollDirection = .Horizontal
}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

override func targetContentOffsetForProposedContentOffset(proposedContentOffset: CGPoint, withScrollingVelocity velocity: CGPoint) -> CGPoint {

let leftThreshold = CGFloat(collectionView!.bounds.size.width) * ((CGFloat(visibleIndex) - 0.5))
let rightThreshold = CGFloat(collectionView!.bounds.size.width) * ((CGFloat(visibleIndex) + 0.5))

let currentHorizontalOffset = collectionView!.contentOffset.x

// If you either traverse far enough in either direction,
// or flicked the scrollview over the horizontal velocity in either direction,
// adjust the visible index accordingly

if currentHorizontalOffset < leftThreshold || velocity.x < -velocityThreshold {
visibleIndex = max(0 , (visibleIndex - 1))
} else if currentHorizontalOffset > rightThreshold || velocity.x > velocityThreshold {
visibleIndex += 1
}

var _proposedContentOffset = proposedContentOffset
_proposedContentOffset.x = CGFloat(collectionView!.bounds.width) * CGFloat(visibleIndex)

return _proposedContentOffset
}
}

关于ios - 为什么collectionView单元格居中只能在一个方向起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37340096/

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