cocoa-touch - 启用预览和分页的 UICollectionView

标签 cocoa-touch ios6 uikit uicollectionview

在 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 不会渲染当前边界之外的任何单元格......破坏了您的预览效果。

解决方案:

  • 创建一个 UICollectionView ,将分页设置为 NO 并使用所需的框架。
  • 创建小于 UICollectionView 的框架/边界的 UICollectionViewCells。在这个阶段,下一个单元格的一部分应该显示在框架中。在执行下面的其他步骤之前,这应该是可见的。
  • 添加一个 collectionView.contentInset.left 和 right(我假设您的布局是水平的)等于 contentOffsetValue 方法(为简单起见,如下所示),以便将第一个和最后一个单元格对齐到中间。
  • 创建一个 UICollectionViewFlowLayout 来覆盖给出停止点的方法,如下所示:

  • 像这样:
    -(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/

    相关文章:

    ios - 无法阻止搜索栏显示取消按钮

    ios - Segue 没有将数据传递给第二个 VC

    ios - 自定义导航栏项目放置不当

    ios - 为什么 Xcode 认为我的变量在应该返回 NSString 时返回了 NSURL?

    ios - 在 Storyboard iOS 中隐藏导航栏的后退按钮

    ios - 如何为UIKit控件设置自定义颜色?

    iphone - 如何以编程方式调用 TouchesBegan、TouchesMoved、TouchedEnded

    iphone - 无法让 Apple Reachability 类正确定义网络

    ios - 加载 xib 时无法识别的选择器

    ios - 使用 UIViewControllerBasedStatusBarAppearance 时更改默认 UIStatusBarStyle