繁体   English   中英

UIPanGestureRecognizer转换-保持两点之间的中心并向其中添加自由变换

[英]UIPanGestureRecognizer translation - maintaining center between two points and adding free transformation to that

我希望一个视图在其他两个视图之间保持中心,但也可以自由移动,将其自身的变换添加到“居中”变换中。 也许可以这样概括一下:视图从零开始,然后应用使其居中的变换。 然后添加本地转换

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

如果视图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方法中的代码,“视图3”将中心保持在1和2之间:

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

谢谢阅读!

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM