在 App Store 中显示搜索结果时,我试图模仿 Apple 的功能。 (引用:http://searchengineland.com/apple-app-search-shows-only-one-result-at-a-time-133818)
它像卡片中的详细应用程序信息一样显示并且它是分页的。当中间有一张事件卡片并且 ScrollView 的分页行为仍然完好时,我一直在思考如何使上一张和下一张卡片显示出来。
我曾尝试使用 UICollectionView 并将 clipSubviews 设置为 NO,希望它能显示上一页和下一页,但是一旦单元格离开屏幕,单元格就会隐藏(从 View 层次结构中删除)而不是显示。我认为这就是 UICollectionView 的享元模式(UICollectionView 的行为)。有什么可能的想法吗?
干杯,
兰迪·普拉纳塔
最佳答案
问题:作为 UIScrollView 的子类的 UICollectionView 本质上通过 bounds.size 的步幅对其边界进行动画处理。虽然这可能意味着您所要做的就是在保持框架更大的同时减少边界,但不幸的是 UICollectionView 不会渲染当前边界之外的任何单元格......破坏了您的预览效果。
解决方案:
像这样:
-(CGFloat)contentOffsetValue
{
return self.collectionView.bounds.size.width * 0.5f - self.itemSize.width * 0.5f;
}
- (CGPoint)targetContentOffsetForProposedContentOffset:(CGPoint)proposedContentOffset withScrollingVelocity:(CGPoint)velocity
{
static float EscapeVelocity = 0.5f; // otherwise snap back to the middle
NSArray* layoutAttributesArray = [self layoutAttributesForElementsInRect:self.collectionView.bounds];
if(layoutAttributesArray.count == 0)
return proposedContentOffset;
CGFloat currentBoundsCenterX = self.collectionView.contentOffset.x + self.collectionView.bounds.size.width * 0.5f;
UICollectionViewLayoutAttributes* candidateNextLayoutAttributes = layoutAttributesArray.firstObject;
for (UICollectionViewLayoutAttributes* layoutAttributes in layoutAttributesArray)
{
if ((layoutAttributes.representedElementCategory != UICollectionElementCategoryCell) ||
(layoutAttributes == candidateNextLayoutAttributes)) // skip the first comparison
continue;
if(velocity.x > EscapeVelocity || velocity.x < -(EscapeVelocity))
{
if(velocity.x > EscapeVelocity && layoutAttributes.center.x > candidateNextLayoutAttributes.center.x)
{
candidateNextLayoutAttributes = layoutAttributes;
}
else if (velocity.x < -(EscapeVelocity) && layoutAttributes.center.x < candidateNextLayoutAttributes.center.x)
{
candidateNextLayoutAttributes = layoutAttributes;
}
}
else
{
if(fabsf(currentBoundsCenterX - layoutAttributes.center.x) < fabsf(currentBoundsCenterX - candidateNextLayoutAttributes.center.x))
{
candidateNextLayoutAttributes = layoutAttributes;
}
}
}
return CGPointMake(candidateNextLayoutAttributes.center.x - self.collectionView.bounds.size.width * 0.5f, proposedContentOffset.y);
}
关于cocoa-touch - 启用预览和分页的 UICollectionView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14744674/