gpt4 book ai didi

ios - 奇怪的 UICollectionViewCell 行为如果 float 大小

转载 作者:行者123 更新时间:2023-11-29 11:43:09 25 4
gpt4 key购买 nike

更新:示例项目是 github link

使用 iPHONE 6 模拟器

实现此错误的 2 种可能方法。只需构建并运行即可查看。或者取消注释256行

//return CGSizeMake(widthAndHeight * self.venueLayoutZoom, widthAndHeight * self.venueLayoutZoom);

然后按“删除列”按钮

我有 UICollectionView 和很多 UICollectionViewCell。我的用户界面让我在单元格之间使用零空间。我的 UICollectionView 中有一个 pinch 手势。所以有时 sizeForItem:atIndexPath: 会返回一个很大的 float (比如 11.821123411231)。问题是:如果我不舍入这些 float ,我有时会出现奇怪的行为 - enter image description here

应该是enter image description here

如果我向上或向下舍入 sizeForItem:atIndexPath: 它看起来不错,但单元格之间有空格 enter image description here

我不知道该怎么办。确实有必要不使用这些空间或奇怪的单元格。我的流程布局是 this

Controller 代码:

- (CGSize)collectionView:(UICollectionView *)collectionView
layout:(UICollectionViewLayout *)collectionViewLayout
sizeForItemAtIndexPath:(nonnull NSIndexPath *)indexPath
{
CGFloat widthAndHeight = [self widthAndHeightForCollectionView:collectionView];
CGFloat result = widthAndHeight * self.venueLayoutZoom;
return CGSizeMake(result, result);
}

- (void)didReceivePinchGesture:(UIPinchGestureRecognizer *)gesture
{
static CGFloat scaleStart;

if (gesture.state == UIGestureRecognizerStateBegan) {
scaleStart = self.venueLayoutZoom;
}
else if (gesture.state == UIGestureRecognizerStateChanged) {

self.venueLayoutZoom = scaleStart * gesture.scale;
}
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
VenueLayoutCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kVenueLayoutCellReuseIdentifier forIndexPath:indexPath];

self.activeCollectionViewCellsDictionary[indexPath] = cell;
if (self.activeCollectionViewObjects.count > indexPath.section) {
NSArray *rows = self.activeCollectionViewObjects[indexPath.section];
if (rows.count > indexPath.row) {
if ([rows[indexPath.row] isKindOfClass:[VenueLayoutCellObject class]]) {
VenueLayoutCellObject *object = rows[indexPath.row];
cell.cellObject = object;

}
}
}
return cell;
}

单元格代码:

@property (nonatomic, strong) CALayer *circularLayer;

- (void)awakeFromNib
{
[super awakeFromNib];
[self allocAndAddLayers];
}

- (void)allocAndAddLayers
{
self.circularLayer = [CALayer layer];
[self.layer addSublayer:self.circularLayer];
}

#pragma mark - Property Set

- (void)setCellObject:(VenueLayoutCellObject *)cellObject
{
self->_cellObject = cellObject;
self.objectBackgroundColor = cellObject.objectColor;
self.type = cellObject.type;
}

- (void)prepareForReuse
{
[self.circularLayer removeFromSuperlayer];
[self allocAndAddLayers];
}

- (void)setType:(VenueLayoutObjectType)type
{
self->_type = type;
[self updateInderfaceDependOnType];
}

- (void)layoutSubviews
{
[CATransaction begin];
[CATransaction setDisableActions:YES];
[self updateRoundedCorners];
[CATransaction commit];
[super layoutSubviews];
}

- (void)updateRoundedCorners
{
CGRect bounds = self.bounds;
CGRect frame = self.frame;
self.circularLayer.frame = CGRectMake(0, 0, frame.size.width, frame.size.height);
}

- (void)setLayerBackgroundColor:(UIColor *)color
{
self.circularLayer.backgroundColor = color.CGColor;
self.objectMaskLayer.strokeColor = color.CGColor;
self.deselectedColor = color;
}

- (void)updateInderfaceDependOnType
{
UIColor *tempObjectColor = [UIColor grayColor];
UIColor *objectColor = self.objectBackgroundColor ? : tempObjectColor;
[self setLayerBackgroundColor:objectColor];
}

更新代码:

- (CGSize)collectionView:(UICollectionView *)collectionView
layout:(UICollectionViewLayout *)collectionViewLayout
sizeForItemAtIndexPath:(nonnull NSIndexPath *)indexPath
{
CGFloat widthAndHeight = self.widthAndHeightForActiveCollectionView;
CGFloat result = lroundf(widthAndHeight * self.venueLayoutZoom);
CGFloat width = result;
if (indexPath.row > self.increasedWidthInitialIndex) {
width++;
}
return CGSizeMake(width, result);
}

CGFloat widthAndHeight = CGRectGetWidth(self.activeCollectionView.bounds) / maxCount;
NSNumber *widthNumber = @(CGRectGetWidth(self.activeCollectionView.bounds));
NSInteger count = widthNumber.integerValue % maxCount;
maxCount--;
self.increasedWidthInitialIndex = maxCount - count;

更新:如果我使用较小的项目大小(例如 CGSizeMake(7.f, 7.f)),单元格不适合整个 Collection View 但空间仍然存在

最佳答案

最终将宽度/高度设置为半像素会根据设备像素深度(视网膜与非视网膜)更改比例 - 更多信息 here

当您尝试将屏幕划分为非整数时,即对于 320 的屏幕宽度,创建 6 个 53.3 的单元格可能会导致 UI 中断。

你应该尝试找到你的剩余部分,即:

320 % 6 -> 2

然后将 2 个单元格加宽 1 个像素。因此,不是由 53.3 像素宽组成的 6 个单元格,而是 2 个由 54 像素组成的单元格和另外 4 个由 53 像素组成的单元格,然后一切都会正确适合。

编辑:

检查完你的项目后,我添加了你的集合的打印输出,看看宽度可能导致什么问题,我在 ViewController.m 中添加了这个方法:

- (void)printoutGrid
{
NSLog(@"collectionview bounds: %f, %@", self.activeCollectionView.bounds.size.width, NSStringFromCGSize(self.activeCollectionView.contentSize));
CGFloat calculatedWidth = [self widthAndHeightForActiveCollectionViewItem];
CGFloat result = floor(calculatedWidth * self.venueLayoutZoom);

for (int rowAt = 0; rowAt < self.activeCollectionViewObjects.count; rowAt++)
{
NSArray* arrayAt = self.activeCollectionViewObjects[rowAt];
NSString* rowString = @"|";

for (int colAt = 0; colAt < arrayAt.count; colAt++)
{
if (colAt > self.increasedWidthInitialIndex)
{
rowString = [rowString stringByAppendingString:[NSString stringWithFormat:@"%d|", (int)result + 1]];
}
else
{
rowString = [rowString stringByAppendingString:[NSString stringWithFormat:@"%d|", (int)result]];
}
}

NSLog(@"%@", rowString);
}
}

并且在每次重新加载集合或使布局无效后调用它,我只发现您应该使用 floor 而不是 lroundf。

此方法将帮助您为将来调试宽度。

但是,在查看您的项目设置后,您的模拟器中似乎出现了问题,我注意到它太大/超出比例并且不是视网膜,我去了您的项目设置并看到您的项目与视网膜不兼容 - 这意味着 UI 不会使用自动缩放 -> 这意味着它将对所有设备使用相同的分辨率 (320 x 518) 以及它的作用,它会拉伸(stretch) UI - 这意味着在 iPhone 6 中它会成倍增加,并且正在拉伸(stretch)反分析,即使那里没有空间,也会在单元格之间造成这些所谓的空白空间。

您需要做的是转到您的项目设置 -> 通常向下滚动到“启动屏幕文件”并从列表中选择“启动屏幕”:

enter image description here

这似乎解决了这个项目的问题。如果您还有任何问题以及它是否有效,请告诉我。

请注意 - 它需要您稍微更改尺寸计算,因为分辨率在“viewDidLayoutSubviews”之后再次更改,而不仅仅是在加载 View 时。

祝你好运!

关于ios - 奇怪的 UICollectionViewCell 行为如果 float 大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45345060/

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