6

UIPanGestureRecognizer を使用してドラッグと回転の計算を行っています。回転角度も正確、ドラグ位置もほぼ正確。問題は、ボックスの中心を回るにつれて、角度に応じて調整する必要があり、その方法がわからないことです.

180度の回転がどのように見えるかの写真を含めましたが、回転中の指の位置. ブロックが指に適切に留まるように調整する方法がわかりません。そして、それは奇妙な動作であるため、明確にするためにビデオを用意しています。http://tinypic.com/r/mhx6a1/5

編集:これは、実際に何が起こっているかを示すビデオです。問題は、iPad のビデオでは指が動いているということです。現実の世界では、指が動いているアイテムの特定の場所に固定されます。必要な計算は、実際の中心とは異なる角度に沿ってタッチ位置を調整することです。私はちょうど数学を理解することはできません。http://tinypic.com/r/4vptnk/5

最初のショット

セカンドショット

サードショット

どうもありがとう!

- (void)handlePan:(UIPanGestureRecognizer *)gesture
{
    if (gesture.state == UIGestureRecognizerStateBegan) {
        // set original center so we know where to put it back if we have to.
        originalCenter = dragView.center;

    } else if (gesture.state == UIGestureRecognizerStateChanged) {
        [dragView setCenter:CGPointMake( originalCenter.x + [gesture translationInView:self.view].x , originalCenter.y + [gesture translationInView:self.view].y )];

        CGPoint p1 = button.center;
        CGPoint p2 = dragView.center;

        float adjacent = p2.x-p1.x;
        float opposite = p2.y-p1.y;

        float angle = atan2f(adjacent, opposite); 

        [dragView setTransform:CGAffineTransformMakeRotation(angle*-1)];

    }
}
4

2 に答える 2

8

私はついにこの問題を解決し、完全に機能するようになりました。持続性ですか??

変更を説明するいくつかのコメントを含むソリューションのコードを次に示します。

- (void)handlePan:(UIPanGestureRecognizer *)gesture
{
    if (gesture.state == UIGestureRecognizerStateBegan) {
        // Get the location of the touch in the view we're dragging.
        CGPoint location = [gesture locationInView:dragView];

        // Now to fix the rotation we set a new anchor point to where our finger touched. Remember AnchorPoints are 0.0 - 1.0 so we need to convert from points to that by dividing
        [dragView.layer setAnchorPoint:CGPointMake(location.x/dragView.frame.size.width, location.y/dragView.frame.size.height)];


    } else if (gesture.state == UIGestureRecognizerStateChanged) {
        // Calculate Our New Angle
        CGPoint p1 = button.center;
        CGPoint p2 = dragView.center;

        float adjacent = p2.x-p1.x;
        float opposite = p2.y-p1.y;

        float angle = atan2f(adjacent, opposite); 

        // Get the location of our touch, this time in the context of the superview.
        CGPoint location = [gesture locationInView:self.view];

        // Set the center to that exact point, We don't need complicated original point translations anymore because we have changed the anchor point.
        [dragView setCenter:CGPointMake(location.x, location.y)];

        // Rotate our view by the calculated angle around our new anchor point.
        [dragView setTransform:CGAffineTransformMakeRotation(angle*-1)];

    }
}

私の1か月以上の闘争と解決策が将来誰かに役立つことを願っています. ハッピーコーディング:)

于 2012-06-04T13:26:10.797 に答える