Xcode-考虑更改的基本x / y位置,四舍五入为最接近的X值

问题描述 投票:0回答:1

我目前正在使用与Garageband iPad Appication差不多的UICollectionViewLayout,它具有可重复使用的装饰/辅助视图。

[主要功能之一是网格线捕捉-使您可以将GREEN单元的X / Y帧位置捕捉到最近的水平/垂直网格线。

我遇到的问题是,基本X / Y位置不是从0,0开始,因为我有一个浮动列和一个浮动行-我的捕捉计算是错误的,因为它不考虑浮动行/浮动列的宽度/高度。

这里是说明我的问题的图像:

snapping/coordinate problem这是我用于计算snapped位置的代码:

CGPoint cellTopLeft = self.frame.origin;
CGFloat gridlineWidth = 30;
CGFloat floatingColumnWidth = 120; // width of the floating left column
CGFloat floatingRowHeight = 100; // height of the floating top row
CGFloat rowHeight = 100; // height of rows on the left (table 0, table 1, table 2, etc)

float snapped_x = [self closest:cellTopLeft.x toValue:gridlineWidth];
float snapped_y = [self closest:cellTopLeft.y toValue:rowHeight];

- (float)closest:(float)input toValue:(float)value {
    return value * floorf((input / value) + 0.5);
}

正如您通过我正在积极拖动的“ Grand 0”绿色单元格所看到的,位置错误。

objective-c uicollectionview uicollectionviewlayout
1个回答
1
投票

感谢@zrzka-有时看似复杂的错误可能是最容易解决的错误,并且只需要额外的一双眼睛。

解决方案是:

float snapped_x = [self closest:cellTopLeft.x - floatingColumnWidth toValue:gridlineWidth] + floatingColumnWidth
float snapped_y = [self closest:cellTopLeft.y - floatingRowHeight toValue:rowHeight] + floatingRowHeight;
© www.soinside.com 2019 - 2024. All rights reserved.