簡體   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