0

我想要一个视图在其他两个视图之间保持中心,但也可以自由移动,将它自己的变换添加到“居中”变换中。也许可以总结为:视图从零开始,然后应用使其居中的变换。然后添加它的局部变换

解释:我有三个视图,我称之为一、二和三。所有三个视图都可以通过平移手势识别器移动。

如果视图一移动,二和三都遵循在我的handlePan方法中基本上使用以下代码:

     if (recognizer.view == One){
        Two.center = CGPointMake(Two.center.x + translation.x, Two.center.y + translation.y);
        Three.center = CGPointMake(Three.center.x + translation.x, Three.center.y + translation.y);
     }

handlePan视图三通过我的方法中的以下代码保持一和二之间的中心:

    float midX = (One.center.x + Two.center.x)/2;
    float midY = (One.center.y + Two.center.y)/2;  
    Three.center = CGPointMake(midX + translation.x, midY+ translation.y);

这一切都很好。问题是我希望视图三移动“自由”并保持该中心,但添加了它自己的转换。看到 illo 因为它可能更容易看到我想要做什么。

在此处输入图像描述

我尝试了以下设置,但它并不完全存在:

     if (recognizer.view == One){
        Two.center = CGPointMake(Two.center.x + translation.x, Two.center.y + translation.y);
        Three.center = CGPointMake(Three.center.x + translation.x, Three.center.y + translation.y);
     } else if (recognizer.view == Two)  {
        float midX = (One.center.x + Two.center.x)/2;
        float midY = (One.center.y + Two.center.y)/2;  
        Three.center = CGPointMake(midX + translation.x, midY+ translation.y);
     }

谢谢阅读!

4

1 回答 1

0

我解决了。很简单。刚刚创建了一个点(这里我使用了两个浮点数 numX 和 numY)来存储视图三和手势识别器产生的翻译。然后在移动视图二时应用它。这段代码在我的handlePan方法中:

 if (recognizer.view == One){
    Two.center = CGPointMake(Two.center.x + translation.x, Two.center.y + translation.y);
    Three.center = CGPointMake(Three.center.x + translation.x, Three.center.y + translation.y);
 } else if (recognizer.view == Two)  {
    float midX = (One.center.x + Two.center.x)/2;
    float midY = (One.center.y + Two.center.y)/2;  
    Three.center = CGPointMake(midX + numX, midY+ numY);
 } else if (recognizer.view == Three) {
    numX = numX+translation.x;
    numY = numY+translation.y;
 }
于 2013-01-25T08:13:13.973 回答