gpt4 book ai didi

ios - 在 UIView 数组中查找边

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:20:52 25 4
gpt4 key购买 nike

有没有办法在 UIViews 的 NSArray 中找到边缘。

例如,下图显示了一组 UIView(1 到 7)。每个 View 邻接另一个 View 。所以 View 1 可能是 (0,0,70,20), View 6 是 (70,0,30,50)。

我将如何返回一个行数组,这些行是 View 之间的分隔符。在下面的示例中,将有 6 个分隔符(内部行)。

    ---------------------
| 1 | 6 |
| | |
-------------| |
| 2 | 3 | |
| |-----| |
| | 4 |------|
-------------| |
| 5 | 7 |
---------------------

我第一次尝试获取每个方 block 的内部线条(不在容器 View 外部的线条),删除重复项(即 3 到 4 之间的线条),

.. 然后删除与边缘上其他线接触的线,直到剩下一条线。不幸的是,这会删除最右边的垂直线。

最后一部分的代码:

NSMutableArray *sidesToDiscard = [[NSMutableArray alloc] init];
for (NSValue *rect1 in self.sides)
{
for (NSValue *rect2 in self.sides)
{
if ([rect1 isEqualToValue:rect2])
{

} else
{
BOOL xIsSame = (rect2.CGRectValue.origin.x == rect1.CGRectValue.origin.x);
BOOL bothAreVertical = (rect2.CGRectValue.size.width == 0 && rect1.CGRectValue.size.width == 0);
BOOL areTouchingOnVertical = ((rect1.CGRectValue.origin.y + rect1.CGRectValue.size.height == rect2.CGRectValue.origin.y) || (rect1.CGRectValue.origin.y == rect1.CGRectValue.origin.y + rect2.CGRectValue.size.height));

BOOL yIsSame = (rect2.CGRectValue.origin.y == rect1.CGRectValue.origin.y);
BOOL bothAreHorizontal = (rect2.CGRectValue.size.height == 0 && rect1.CGRectValue.size.height == 0);
BOOL areTouchingOnHorizontal = ((rect1.CGRectValue.origin.x + rect1.CGRectValue.size.width == rect2.CGRectValue.origin.x) || (rect1.CGRectValue.origin.x == rect2.CGRectValue.origin.x + rect2.CGRectValue.size.width));

if (((xIsSame && bothAreVertical) && areTouchingOnVertical) || ((yIsSame && bothAreHorizontal) && areTouchingOnHorizontal))
{
// if are touching then remove, leaving one left...
[sidesToDiscard addObject:rect1];
[sidesToDiscard addObject:rect2];
}
}
}
}
[self.sides removeObjectsInArray:sidesToDiscard];

最佳答案

您可以将每个 View (矩形)转换为 4 个端点。删除重复的点。由于每条线段都是由两点连接而成,所以可以通过遍历所有可能的点对来遍历所有可能的线段,而你想找到的线段是

  1. 线段上的每个点都与一些 View 相邻
  2. 尽可能长
  3. 垂直或水平
  4. 不在容器 View 之外

要检查 1,您必须遍历 View 的所有边,移除线段和边的交点。如果什么都没有留下,您就知道它可能就是您想要的线段。

3 和 4 很容易检查。

遍历所有满足1,3,4的线段,如果有两条线段相交,去掉较短的那条。剩下的必须满足2。

关于ios - 在 UIView 数组中查找边,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18384751/

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