我想在我的应用程序中实现“卡片”,比如Safari标签或app Store搜索。
我将在屏幕中央显示用户一张卡,在左侧和右侧显示前一张和下一张卡的一部分。(例如,参见App Store搜索或Safari标签)
我决定使用UICollectionView,我需要改变页面大小(不知道怎么做)或者实现自己的布局子类(不知道怎么做)?
有什么需要帮忙的吗?
发布于 2013-04-18 05:28:31
下面是我找到的获得这种效果的最简单的方法。它包括你的收藏视图和一个额外的秘密滚动视图。
设置您的收藏视图
contentInset_collectionView.contentInset = UIEdgeInsetsMake(0,(self.view.frame.size.width-pageSize)/2,0,pageSize这有助于正确地偏移单元格。
设置您的秘密滚动视图
hidden。移动您的手势识别器
_collectionView addGestureRecognizer:_secretScrollView.panGestureRecognizer;_collectionView.panGestureRecognizer.enabled = NO;
委派
- (void) scrollViewDidScroll:(UIScrollView *)scrollView {
CGPoint contentOffset = scrollView.contentOffset;
contentOffset.x = contentOffset.x - _collectionView.contentInset.left;
_collectionView.contentOffset = contentOffset;
}随着滚动视图的移动,获取它的偏移量,并将其设置为集合视图的偏移量。
我在这里写了这个博客,所以请查看以下链接以获取更新:http://khanlou.com/2013/04/paging-a-overflowing-collection-view/
发布于 2014-03-17 20:39:00
您可以子类化UICollectionViewFlowLayout并覆盖,如下所示:
- (CGPoint)targetContentOffsetForProposedContentOffset:(CGPoint)contentOffset
withScrollingVelocity:(CGPoint)velocity
{
NSArray* layoutAttributesArray =
[self layoutAttributesForElementsInRect:self.collectionView.bounds];
if(layoutAttributesArray.count == 0)
return contentOffset;
UICollectionViewLayoutAttributes* candidate =
layoutAttributesArray.firstObject;
for (UICollectionViewLayoutAttributes* layoutAttributes in layoutAttributesArray)
{
if (layoutAttributes.representedElementCategory != UICollectionElementCategoryCell)
continue;
if((velocity.x > 0.0 && layoutAttributes.center.x > candidate.center.x) ||
(velocity.x <= 0.0 && layoutAttributes.center.x < candidate.center.x))
candidate = layoutAttributes;
}
return CGPointMake(candidate.center.x - self.collectionView.bounds.size.width * 0.5f, contentOffset.y);
}这将根据速度获得下一个或上一个单元格...但是,它不会捕捉到当前单元格。
发布于 2016-08-17 21:07:35
Swift…中的@Mike M's answer
class CenteringFlowLayout: UICollectionViewFlowLayout {
override func targetContentOffsetForProposedContentOffset(proposedContentOffset: CGPoint, withScrollingVelocity velocity: CGPoint) -> CGPoint {
guard let collectionView = collectionView,
let layoutAttributesArray = layoutAttributesForElementsInRect(collectionView.bounds),
var candidate = layoutAttributesArray.first else { return proposedContentOffset }
layoutAttributesArray.filter({$0.representedElementCategory == .Cell }).forEach { layoutAttributes in
if (velocity.x > 0 && layoutAttributes.center.x > candidate.center.x) ||
(velocity.x <= 0 && layoutAttributes.center.x < candidate.center.x) {
candidate = layoutAttributes
}
}
return CGPoint(x: candidate.center.x - collectionView.bounds.width / 2, y: proposedContentOffset.y)
}
}https://stackoverflow.com/questions/15919457
复制相似问题