gpt4 book ai didi

ios - 具有自定义流布局的 CollectionView 不会触发 didSelectItemAt

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

我已经创建了一个包含 GSMMap 和 collectionView(Horizo​​ntal) 的 viewController,就像 google map 一样。我使用下面的代码使项目居中并对齐。

class ZoomAndSnapFlow: UICollectionViewFlowLayout {

let activeDistance: CGFloat = 200
let zoomFactor: CGFloat = 0.3

override init() {
super.init()

scrollDirection = .horizontal
minimumLineSpacing = 40
itemSize = CGSize(width: 200, height: 157)
}

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

override func prepare() {
guard let collectionView = collectionView else { fatalError() }
let verticalInsets = (collectionView.frame.height - collectionView.adjustedContentInset.top - collectionView.adjustedContentInset.bottom - itemSize.height) / 2
let horizontalInsets = (collectionView.frame.width - collectionView.adjustedContentInset.right - collectionView.adjustedContentInset.left - itemSize.width) / 2
sectionInset = UIEdgeInsets(top: verticalInsets, left: horizontalInsets, bottom: verticalInsets, right: horizontalInsets)

super.prepare()
}

override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
guard let collectionView = collectionView else { return nil }
let rectAttributes = super.layoutAttributesForElements(in: rect)!.map { $0.copy() as! UICollectionViewLayoutAttributes }
let visibleRect = CGRect(origin: collectionView.contentOffset, size: collectionView.frame.size)

// Make the cells be zoomed when they reach the center of the screen
for attributes in rectAttributes where attributes.frame.intersects(visibleRect) {
let distance = visibleRect.midX - attributes.center.x
let normalizedDistance = distance / activeDistance

if distance.magnitude < activeDistance {
let zoom = 1 + zoomFactor * (1 - normalizedDistance.magnitude)
attributes.transform3D = CATransform3DMakeScale(zoom, zoom, 1)
attributes.zIndex = Int(zoom.rounded())
}
}

return rectAttributes
}

override func targetContentOffset(forProposedContentOffset proposedContentOffset: CGPoint, withScrollingVelocity velocity: CGPoint) -> CGPoint {
guard let collectionView = collectionView else { return .zero }

// Add some snapping behaviour so that the zoomed cell is always centered
let targetRect = CGRect(x: proposedContentOffset.x, y: 0, width: collectionView.frame.width, height: collectionView.frame.height)
guard let rectAttributes = super.layoutAttributesForElements(in: targetRect) else { return .zero }

var offsetAdjustment = CGFloat.greatestFiniteMagnitude
let horizontalCenter = proposedContentOffset.x + collectionView.frame.width / 2

for layoutAttributes in rectAttributes {
let itemHorizontalCenter = layoutAttributes.center.x
if (itemHorizontalCenter - horizontalCenter).magnitude < offsetAdjustment.magnitude {
offsetAdjustment = itemHorizontalCenter - horizontalCenter
}
}

return CGPoint(x: proposedContentOffset.x + offsetAdjustment, y: proposedContentOffset.y)
}

override func shouldInvalidateLayout(forBoundsChange newBounds: CGRect) -> Bool {
// Invalidate layout so that every cell get a chance to be zoomed when it reaches the center of the screen
return true
}

override func invalidationContext(forBoundsChange newBounds: CGRect) -> UICollectionViewLayoutInvalidationContext {
let context = super.invalidationContext(forBoundsChange: newBounds) as! UICollectionViewFlowLayoutInvalidationContext
context.invalidateFlowLayoutDelegateMetrics = newBounds.size != collectionView?.bounds.size
return context
}

}

我有两个问题:

  1. scrollViewDidEndDecelerating 方法中,我想检测选择了哪个项目来更改 map 相机,但是当我使用 collectionView.indexPathForItem(at: collectionView.center) 时,它返回零我检查了坐标。它是项目的中心

  2. didSelectItemAt 根本不触发。委托(delegate)已设置(滚动委托(delegate)有效)

最佳答案

第一个问题:

let centerX = collectionView.contentOffset.x + collectionView.frame.width * 0.5
let centerY = collectionView.contentOffset.y + collectionView.frame.height * 0.5
collectionView.indexPathForItem(at: CGPoint(x: centerX, y: centerY))

第二个:

didSelectItemAt() 只是在 gestureRecognizer 事件时触发,所以你应该手动调用任何你想要的。希望这对您有所帮助。

关于ios - 具有自定义流布局的 CollectionView 不会触发 didSelectItemAt,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58691423/

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