gpt4 book ai didi

ios - 如何使用自定义布局修复滚动到 UICollectionView 中的可见单元格?

转载 作者:可可西里 更新时间:2023-11-01 03:11:19 24 4
gpt4 key购买 nike

我正在研究受 UIPickerView 启发的自定义值选择器。它看起来像这样: Technical prototype screen 1

如您所见,此选择器的主要特征之一是中央单元格,它应该比其他单元格更宽,以使其在中央框架旁边可见。当您使用平移手势滚动选择器时,它应该动态更改中心值并根据上述逻辑调整单元格。它工作得非常完美: Technical prototype screen 2

问题出在点击手势上。当用户通过点击它来选择选择器上的任何项目时,选择器会尝试滚动到该项目。但是由于自定义布局更改了它的偏移量,因此 UIScrollView 滚动到错误的点。它看起来像这样: Technical prototype screen 3

当我尝试滚动到屏幕外的单元格时,一切正常 - 该单元格不受布局影响,并且它的坐标是正确的。该问题仅针对可见单元格出现。我完全不知道如何解决这个问题。你可以在这里找到整个项目:Carousel Collection View Test Project

请在下面找到一些重要的代码。

// CarouselLayout.m

- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect {
NSArray *array = [super layoutAttributesForElementsInRect:rect];
CGRect visibleRect;
visibleRect.origin = self.collectionView.contentOffset;
visibleRect.size = self.collectionView.bounds.size;

CGRect center = CGRectMake(CGRectGetMidX(visibleRect) - 1.0, 0.0, 2.0, CGRectGetHeight(visibleRect));
CGFloat coreWidth = CGRectGetWidth(self.centralFrame) / 3.0;

for (UICollectionViewLayoutAttributes *attributes in array) {
if (CGRectIntersectsRect(attributes.frame, rect)){
CGFloat distance = CGRectGetMidX(visibleRect) - attributes.center.x;
CGFloat offset = 0.0;
CGRect coreFrame = CGRectMake(attributes.center.x - (coreWidth / 2.0), 0.0, coreWidth, CGRectGetHeight(self.centralFrame));
if (CGRectIntersectsRect(center, coreFrame)) {
if (attributes.indexPath.item % 2 == 0) {
self.centralItemOffset = (CGRectGetWidth(self.centralFrame) - CGRectGetWidth(attributes.frame) - 4.0) / 2.0;
if ([self.collectionView.delegate respondsToSelector:@selector(collectionView:layout:didChangeCentralItem:)]) {
[(id <CarouselLayoutDelegate>)self.collectionView.delegate collectionView:self.collectionView layout:self didChangeCentralItem:attributes.indexPath];
}
}
}
offset = (distance > 0) ? -self.centralItemOffset : self.centralItemOffset;
attributes.center = CGPointMake(attributes.center.x + offset, attributes.center.y);
}
}
return array;
}

- (CGPoint)targetContentOffsetForProposedContentOffset:(CGPoint)proposedContentOffset withScrollingVelocity:(CGPoint)velocity {
CGFloat offsetAdjustment = MAXFLOAT;
CGFloat horizontalCenter = proposedContentOffset.x + (CGRectGetWidth(self.collectionView.bounds) / 2.0);
CGRect targetRectHorizontal = CGRectMake(proposedContentOffset.x, 0.0, self.collectionView.bounds.size.width, self.collectionView.bounds.size.height);

NSArray *array = [super layoutAttributesForElementsInRect:targetRectHorizontal];
for (UICollectionViewLayoutAttributes *attributes in array) {
if (attributes.indexPath.item % 2 == 1) {
continue;
}
CGFloat itemHorizontalCenter = attributes.center.x;
if (ABS(itemHorizontalCenter - horizontalCenter) < ABS(offsetAdjustment)) {
offsetAdjustment = itemHorizontalCenter - horizontalCenter;
}
}

return CGPointMake(proposedContentOffset.x + offsetAdjustment, proposedContentOffset.y);
}

// ViewController.m

- (void)collectionView:(UICollectionView *)collectionView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.item % 2 == 1) {
return;
}
NSString *nextValue = [self valueAtIndexPath:indexPath];
[self scrollToValue:nextValue animated:YES];
self.currentValue = nextValue;
}

- (void)scrollToValue:(NSString *)value animated:(BOOL)animated {
NSIndexPath *targetPath = nil;
NSIndexPath *currentPath = nil;
for (NSString *item in self.itemsArray) {
if (!targetPath && [value isEqualToString:item]) {
targetPath = [NSIndexPath indexPathForItem:([self.itemsArray indexOfObject:item] * 2) inSection:0];
}
if (!currentPath && [self.currentValue isEqualToString:item]) {
currentPath = [NSIndexPath indexPathForItem:([self.itemsArray indexOfObject:item] * 2) inSection:0];
}
if (targetPath && currentPath) {
break;
}
}
if (targetPath && currentPath) {
[self.itemsCollectionView scrollToItemAtIndexPath:targetPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:animated];
}
}

如果还不够,请在评论中索取额外的代码。

最佳答案

scrollToValue 方法中更改:

if (targetPath && currentPath) {
[self.itemsCollectionView scrollToItemAtIndexPath:targetPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:animated];
}

进入:

if (targetPath && currentPath) {
if (targetPath.row < currentPath.row) {
[self.itemsCollectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:self.itemsArray.count inSection:0] atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:animated];
[self.itemsCollectionView scrollToItemAtIndexPath:targetPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:animated];
} else {
[self.itemsCollectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:animated];
[self.itemsCollectionView scrollToItemAtIndexPath:targetPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:animated];

}
}

它会起作用。

关于ios - 如何使用自定义布局修复滚动到 UICollectionView 中的可见单元格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27652749/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com