简体   繁体   English

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

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

I want a view to maintain center between two other views but also move freely, adding it's own transform to the "centering" transform. 我希望一个视图在其他两个视图之间保持中心,但也可以自由移动,将其自身的变换添加到“居中”变换中。 Maybe it can be summed up by saying: The view starts at zero, Then the transform that makes it centered is applied. 也许可以这样概括一下:视图从零开始,然后应用使其居中的变换。 Then it's local transform is added 然后添加本地转换

Explanation : I have three views that I'll call One, Two and Three. 说明 :我有三个视图,分别称为一,二和三。 All three views can be moved via pan gesture recognizer. 可以通过平移手势识别器移动所有三个视图。

If view One moves, both Two and Three follow using basically the code below in my handlePan method: 如果视图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);
     }

View Three maintains center between One and Two via the code below in my handlePan method: 通过以下我的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);

All this works just fine. 所有这些都很好。 The issue is I want view three to move "free" and also maintain that center, but with it's own transformation added. 问题是我希望视图三“自由”移动并保持该中心不变,但是添加了自己的转换。 See the illo as it might be easier to see what I want to do. 查看illo,因为它可能更容易了解我想要做什么。

在此处输入图片说明

I tried the following setup, but it's not quite there: 我尝试了以下设置,但还不足够:

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

Thanks for reading! 谢谢阅读!

I worked it out. 我解决了。 Pretty simple. 很简单 Just created a point (here I used two floats numX and numY) to store the translation produced by view Three and the gesture recognizer. 刚刚创建了一个点(这里我使用了两个浮点数numX和numY)来存储视图3和手势识别器产生的转换。 Then apply that when moving view Two. 然后在移动第二视图时应用它。 This code is in my handlePan method: 这段代码在我的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