UIPanGestureRecognizer翻译 - 维护两点之间的中心并为其添加自由变换

时间:2013-01-24 15:54:35

标签: ios transformation uipangesturerecognizer

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

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

如果查看One移动,则两个和三个基本上遵循我的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,因为它可能更容易看到我想要做什么。

enter image description here

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

     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);
     }

感谢阅读!

1 个答案:

答案 0 :(得分:0)

我解决了这个问题。很简单。刚刚创建了一个点(这里我使用了两个浮点数numX和numY)来存储由视图Three和手势识别器生成的转换。然后在移动视图二时应用它。此代码位于我的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;
 }