【问题标题】:Positioning subview with arbitrary anchoring point in superview在超级视图中使用任意锚点定位子视图
【发布时间】:2019-06-09 12:00:59
【问题描述】:

我很难找出合适的算法。我有一个超级视图(绿色)和一个子视图(黄色)。在这个子视图(红色)中有一个任意的锚点(只是一个选择的点,不与图层的anchor 属性混合)我必须以这个红点放置的方式放置并调整子视图的大小在父视图的中心,如果需要,子视图会按比例调整大小(如果在移动其框架后超出了父视图的范围)。未使用自动布局。看看我想要得到的图片。

所以基本上我写了一个 C++ 测试例程,在给定新点的“周围”计算一个新的比例矩形(所以这个点成为这个新矩形的中心),但我不知道接下来要做什么来处理一个点不在子视图的中心。 Rect 基本上是一个带有 x, y, width, height 成员的结构。

    Rect
    calculateRectInBounds(const Rect &boundingRect, const Rect &initialRect, const Point &translationPoint)
    {
        double boundsWidth = boundingRect.getWidth();
        double boundsHeight = boundingRect.getHeight();
        double distanceX, distanceY;
        Size sizeByWidth, sizeByHeight;

        // detect part where point is

        /*
         * UL | UR
         * ---C---
         * LL | LR
         */

        // -- upper left - check left & top borders

        if (translationPoint.x <= boundsWidth / 2 && translationPoint.y < boundsHeight / 2) {
            distanceX = translationPoint.x;
            distanceY = translationPoint.y;

            sizeByWidth = scaleSizeToWidth(initialRect.getSize(), distanceX);
            sizeByHeight = scaleSizeToHeight(initialRect.getSize(), distanceY);

            if (sizeByWidth.height > distanceY) {
                return {translationPoint.x - sizeByHeight.width,
                        0,
                        sizeByHeight.width * 2,
                        sizeByHeight.height * 2};
            }

            return {0,
                    translationPoint.y - sizeByWidth.height,
                    sizeByWidth.width * 2,
                    sizeByWidth.height * 2};
        }

        // -- upper right - check top & right borders

        if (translationPoint.x > boundsWidth / 2 && translationPoint.y <= boundsHeight / 2) {
            distanceX = boundsWidth - translationPoint.x;
            distanceY = translationPoint.y;

            sizeByWidth = scaleSizeToWidth(initialRect.getSize(), distanceX);
            sizeByHeight = scaleSizeToHeight(initialRect.getSize(), distanceY);

            if (sizeByWidth.height > distanceY) {
                return {translationPoint.x - sizeByHeight.width,
                        0,
                        sizeByHeight.width * 2,
                        sizeByHeight.height * 2};
            }

            return {translationPoint.x - sizeByWidth.width,
                    translationPoint.y - sizeByWidth.height,
                    sizeByWidth.width * 2,
                    sizeByWidth.height * 2};
        }

        // -- lower right - check right & bottom borders

        if (translationPoint.x >= boundsWidth / 2 && translationPoint.y > boundsHeight / 2) {
            distanceX = boundsWidth - translationPoint.x;
            distanceY = boundsHeight - translationPoint.y;

            sizeByWidth = scaleSizeToWidth(initialRect.getSize(), distanceX);
            sizeByHeight = scaleSizeToHeight(initialRect.getSize(), distanceY);

            if (sizeByWidth.height > distanceY) {
                return {translationPoint.x - sizeByHeight.width,
                        translationPoint.y - sizeByHeight.height,
                        sizeByHeight.width * 2,
                        sizeByHeight.height * 2};
            }

            return {translationPoint.x - sizeByWidth.width,
                    translationPoint.y - sizeByWidth.height,
                    sizeByWidth.width * 2,
                    sizeByWidth.height * 2};
        }

        // -- lower left - check bottom & left borders

        if (translationPoint.x < boundsWidth / 2 && translationPoint.y >= boundsHeight / 2) {
            distanceX = translationPoint.x;
            distanceY = boundsHeight - translationPoint.y;

            sizeByWidth = scaleSizeToWidth(initialRect.getSize(), distanceX);
            sizeByHeight = scaleSizeToHeight(initialRect.getSize(), distanceY);

            if (sizeByWidth.height > distanceY) {
                return {translationPoint.x - sizeByHeight.width,
                        translationPoint.y - sizeByHeight.height,
                        sizeByHeight.width * 2,
                        sizeByHeight.height * 2};
            }

            return {0,
                    translationPoint.y - sizeByWidth.height,
                    sizeByWidth.width * 2,
                    sizeByWidth.height * 2};
        }

        // -- center

        return initialRect;
    }

Size
scaleSizeToWidth(Size size, double newWidth)
{
    return {newWidth, (std::min(size.width, newWidth) / std::max(size.width, newWidth)) * size.height};
}

Size
scaleSizeToHeight(Size size, double newHeight)
{
    return {(std::min(size.height, newHeight) / std::max(size.height, newHeight)) * size.width, newHeight};
}

编辑

感谢@MBo 正确例程的回答

    Rect
    calculateRectInBounds(const Rect &boundingRect, const Rect &initialRect, const Point &anchorPoint)
    {
        auto lDist = anchorPoint.x - initialRect.getX();
        auto rDist = initialRect.getWidth() - lDist;
        auto tDist = anchorPoint.y - initialRect.getY();
        auto bDist = initialRect.getHeight() - tDist;

        auto lRatio = (lDist * 2) / boundingRect.getWidth();
        auto rRatio = (rDist * 2) / boundingRect.getWidth();
        auto tRatio = (tDist * 2) / boundingRect.getHeight();
        auto bRatio = (bDist * 2) / boundingRect.getHeight();

        auto scale = 1 / std::max({lRatio, rRatio, tRatio, bRatio});

        auto x = initialRect.getWidth() / 2 - lDist * scale + initialRect.getX();
        auto y = initialRect.getHeight() / 2 - tDist * scale + initialRect.getY();
        auto width = initialRect.getWidth() * scale;
        auto height = initialRect.getHeight() * scale;

        return {x, y, width, height};
    }

【问题讨论】:

  • 1) 将带有红点的子视图移动到视图中心 2) 计算从红点到子视图角度的向量 3) 计算延长线 {red_point, sub_view_angle} 和视图边界之间的交点 4) 将 sub_view 缩放到在点 (3) 处使用红点作为缩放锚点的最少所需扩展。如果我没记错的话,这个算法应该会导致你想要实现的目标。它可能需要一些小的调整。

标签: c++ algorithm geometry


【解决方案1】:

获取从红点到子视图矩形所有边缘的距离

RedX, YellowWidth-RedX, RedY, YellowHeight - RedY

并计算 4 个与超级视图大小的比率

L = (2 * RedX) / GreenWidth
R = (2 * (YellowWidth-RedX)) / GreenWidth
T = (2 * RedY) / GreenHeight
B = (2 * (YellowHeight-RedY)) / GreenHeight

然后找出其中的最大值

Mx = Max(L,R,T,B)

现在确定比例:

Scale = 1 / Mx 

新的子视图坐标:

X =  YellowWidth / 2  - (RedX * Scale)
Y =  YellowHeight / 2  - (RedY * Scale)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-12
    • 1970-01-01
    相关资源
    最近更新 更多