万一其他人偶然发现了这个问题,答案是否定的,我不应该制作自定义 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;
}