我目前正在为iOS项目开发图片库。该项目完全使用自动布局。
我有UICollectionView
并使用全屏细胞。为了保持可见单元格,我在旋转之前存储indexPath并在布局子视图之后选择所需的单元格。
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
NSUInteger itemIndex = [[self collectionView] contentOffset].x / CGRectGetWidth([[self collectionView] bounds]);
[self setCurrentIndexPath:[NSIndexPath indexPathForItem:itemIndex inSection:0]];
}
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
[[self collectionView] scrollToItemAtIndexPath:[self currentIndexPath]
atScrollPosition:UICollectionViewScrollPositionCenteredVertically | UICollectionViewScrollPositionCenteredHorizontally
animated:NO];
}
这一切都很有效且稳定。但是当选择第二个或下一个单元格并旋转时,第一个单元格会在新位置滑动时略微覆盖当前单元格。
我现在想要隐藏所有的屏幕外细胞。所以这就是我到目前为止所做的:
- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
[cell setHidden:NO];
}
- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
[cell setHidden:YES];
}
第二种方法是在集合视图上方有一个覆盖视图,然后在它前面有单元格图像视图:
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
NSUInteger itemIndex = [[self collectionView] contentOffset].x / CGRectGetWidth([[self collectionView] bounds]);
[self setCurrentIndexPath:[NSIndexPath indexPathForItem:itemIndex inSection:0]];
JFWImageViewCollectionViewCell *cell = (JFWImageViewCollectionViewCell *)[[self collectionView] cellForItemAtIndexPath:[self currentIndexPath]];
[[[cell imageView] layer] setZPosition:2];
if (!_rotationCoverView) {
[self setRotationCoverView:[UIView new]];
[[self rotationCoverView] setBackgroundColor:[UIColor blackColor]];
[[self rotationCoverView] setTranslatesAutoresizingMaskIntoConstraints:NO];
[[self rotationCoverView] layer] setZPosition:1];
[[self view] addSubview:[self rotationCoverView]];
[[self view] addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[cover]|"
options:0
metrics:nil
views:@{@"cover": [self rotationCoverView]}]];
[[self view] addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[cover]|"
options:0
metrics:nil
views:@{@"cover": [self rotationCoverView]}]];
}
}
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
JFWImageViewCollectionViewCell *cell = (JFWImageViewCollectionViewCell *)[[self collectionView] cellForItemAtIndexPath:[self currentIndexPath]];
[[[cell imageView] layer] setZPosition:0];
[[self rotationCoverView] removeFromSuperview];
[self setRotationCoverView:nil];
}
我尝试了zPosition
以及-[UIView sendSubViewToFront:]
及其兄弟姐妹的各种组合。
那么如何隐藏所有屏幕外单元格或逐层定位呢?