我正在尝试在我的应用程序中创建卡片布局,就像iOS 7中的多任务UI一样。工程WWDC视频探索iOS 7上的滚动视图,注意到他们使用嵌套滚动视图在iOS 7中实现该效果,但没有详细说明。我想他们会使用UICollectionView,因为这似乎是最直观的方式。
到目前为止,我已尝试制作自定义UICollectionViewFlowLayout,但这很困难,并且不提供我正在寻找的功能。我还尝试使用带有UICollectionViewFlowLayout的UICollectionView,其中每个自定义UICollectionViewCells单元格都有一个滚动视图,其中有一张卡片作为其子视图。我正在使用Autolayout,所以我添加了卡片底部与滚动视图顶部相邻的约束。然后我在scrollview的内容大小中添加了额外的空间,以便当用户向上滑动时,卡片将显示为滚出屏幕。像这样,
[self.contentView addConstraints: [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-70-[cardView(400)]-650-|"
options:0
metrics:nil
views:views]];
由|
指定的超级视图是填充单元格的滚动视图。
而且我也无法使sizeForItemAtIndexPath
足够大以覆盖整个屏幕,因为我收到错误:
the behavior of the UICollectionViewFlowLayout is not defined because:
the item height must be less that the height of the UICollectionView minus the section insets top and bottom values.
所以问题是,我应该制作一个自定义的UICollectionViewFlowLayout而不是尝试利用嵌套的滚动视图吗?
答案 0 :(得分:1)
如果其他人偶然发现了这个问题,答案是否定的,我不应该制作自定义UICollectionViewFlowLayout来处理滑出的手势。使用UIScrollViews比实现自定义布局更自然。它提供与滚动视图在整个iOS中相同的滚动和动量。
我设法用嵌套的UIScrollViews以一种非常简单的方式解决了我遇到的问题。我似乎收到的警告实际上只是将收集视图放在选项卡视图控制器上的第一个选项卡点并让单元占据整个屏幕的错误。通过在集合视图后面添加额外的视图来解决此错误。
- (void)viewDidLoad
{
[super viewDidLoad];
[self.cardCollectionView registerClass:[SwipeCardCollectionViewCell class] forCellWithReuseIdentifier:kCollectionViewCellIdentifier];
self.cardCollectionView.alwaysBounceVertical = NO;
self.cardCollectionView.alwaysBounceHorizontal = YES;
}
- (void)loadView {
self.view = [[UIView alloc] init];
self.layout = [[UICollectionViewFlowLayout alloc] init];
self.layout.scrollDirection = UICollectionViewScrollDirectionHorizontal
self.cardCollectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:self.layout];
self.cardCollectionView.translatesAutoresizingMaskIntoConstraints = NO;
self.cardCollectionView.backgroundColor = [UIColor clearColor];
self.cardCollectionView.delegate = self;
self.cardCollectionView.dataSource = self;
[self.view addSubview:self.cardCollectionView];
NSDictionary *views = @{@"cardCollectionView": self.cardCollectionView}
[self.view addConstraints: [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[cardCollectionView]|"
options:0
metrics:nil
views:views]];
[self.view addConstraints: [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[cardCollectionView]|"
options:0
metrics:nil
views:views]];
views:views]];
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
SwipeCardCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCollectionViewCellIdentifier forIndexPath:indexPath];
cell.delegate = self;
return cell;
}
我使集合视图的sizeForItemAtIndexPath:
等于屏幕的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
return CGSizeMake(self.cardCollectionView.frame.size.width - CARD_WIDTH_INSET, self.cardCollectionView.frame.size.height);
}
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
return 0.0;
}
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
return CARD_SPACING;
}
我通过实现scrollViewWillEndDragging:withVelocity:targetContentOffset:
并在集合视图的任一侧添加内容插入来获取卡片的宽度,而不是集合视图边界的宽度。
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
CGFloat inset = CARD_WIDTH_INSET/2.0;
return UIEdgeInsetsMake(0.0, inset, 0.0, inset);
}
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset {
CGFloat cardWidth = self.cardCollectionView.frame.size.width - CARD_WIDTH_INSET;
NSInteger cardNumber = round(targetContentOffset->x/(cardWidth + CARD_SPACING));
CGFloat finalOffset = cardNumber*(cardWidth + CARD_SPACING);
targetContentOffset->x = finalOffset;
}
最后,通过向我制作的自定义滑动单元添加滚动视图,我能够将卡片刷掉屏幕。启用分页允许卡在屏幕上直接滑动,就像在跳板多任务UI中一样。像这样,
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.cardContainerScrollView = [[UIScrollView alloc] init];
self.cardContainerScrollView.delegate = self;
self.cardContainerScrollView.pagingEnabled = YES;
self.cardContainerScrollView.translatesAutoresizingMaskIntoConstraints = NO;
self.cardContainerScrollView.alwaysBounceHorizontal = NO;
self.cardContainerScrollView.alwaysBounceVertical = YES;
self.cardContainerScrollView.showsHorizontalScrollIndicator = NO;
self.cardContainerScrollView.showsVerticalScrollIndicator = NO;
self.cardView = [[UIView alloc] init];
self.cardView.translatesAutoresizingMaskIntoConstraints = NO;
self.cardView.backgroundColor = [UIColor whiteColor];
[self.cardContainerScrollView addSubview:self.cardView];
[self.contentView addSubview:self.cardContainerScrollView];
NSDictionary *views = @{@"cardView": self.cardView,
@"cardContainerScrollView": self.cardContainerScrollView};
[self.contentView addConstraints: [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[cardContainerScrollView]|"
options:0
metrics:nil
views:views]];
[self.contentView addConstraints: [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[cardContainerScrollView]|"
options:0
metrics:nil
views:views]];
[self.contentView addConstraints: [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-70-[cardView(400)]-650-|"
options:0
metrics:nil
views:views]];
[self.contentView addConstraints: [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[cardView(260)]|"
options:0
metrics:nil
views:views]];
}
return self;
}