0

嗨,我有一张像桌子圆顶的图像。当用户从左向右滑动时,我想顺时针移动它,当用户从右向左滑动时,我想逆时针移动它。

就像实时移动圆桌一样。如何在应用程序中执行此操作?

我正在使用以下代码进行旋转。它来自轨迹球示例。我遇到的问题是当图像旋转时,它会改变它的位置。

- (CATransform3D)rotationTransformForLocation:(CGPoint)location 
{  
    CGFloat trackBallCurrentPoint[3] = {location.x - trackBallCenter.x, location.y - trackBallCenter.y, 0.0f};

    if(fabs(trackBallCurrentPoint[0] - trackBallStartPoint[0]) < kTol && fabs(trackBallCurrentPoint[1] - trackBallStartPoint[1]) < kTol) 
    {
        return CATransform3DIdentity;
    }

    CGFloat dist = trackBallCurrentPoint[0] * trackBallCurrentPoint[0] + trackBallCurrentPoint[1] * trackBallCurrentPoint[1];
    if(dist > trackBallRadius * trackBallRadius) 
    {
        // outside the center of the sphere so make it zero
        trackBallCurrentPoint[2] = 0.0f;
    } 
    else 
    {
        trackBallCurrentPoint[2] = sqrt(trackBallRadius * trackBallRadius - dist);
    }

    // cross product yields the rotation vector
    CGFloat rotationVector[3];
    rotationVector[0] =  trackBallStartPoint[1] * trackBallCurrentPoint[2] - trackBallStartPoint[2] * trackBallCurrentPoint[1];
    rotationVector[1] = -trackBallStartPoint[0] * trackBallCurrentPoint[2] + trackBallStartPoint[2] * trackBallCurrentPoint[0];
    rotationVector[2] =  trackBallStartPoint[0] * trackBallCurrentPoint[1] - trackBallStartPoint[1] * trackBallCurrentPoint[0];

    // calc the angle between the current point vector and the starting point vector
    // use arctan so we get all eight quadrants instead of just the positive ones

    // cos(a) = (start . current) / (||start|| ||current||)
    // sin(a) = (||start X current||) / (||start|| ||current||)
    // a = atan2(sin(a), cos(a))
    CGFloat startLength = sqrt(trackBallStartPoint[0] * trackBallStartPoint[0] + trackBallStartPoint[1] * trackBallStartPoint[1] + trackBallStartPoint[2] * trackBallStartPoint[2]);
    CGFloat currentLength = sqrt(trackBallCurrentPoint[0] * trackBallCurrentPoint[0] + trackBallCurrentPoint[1] * trackBallCurrentPoint[1] + trackBallCurrentPoint[2] * trackBallCurrentPoint[2]);
    CGFloat startDotCurrent = trackBallStartPoint[0] * trackBallCurrentPoint[0] + trackBallStartPoint[1] * trackBallCurrentPoint[1] + trackBallStartPoint[2] * trackBallCurrentPoint[2]; // (start . current)

    // start X current we have already calcualted in the rotation vector
    CGFloat rotationLength = sqrt(rotationVector[0] * rotationVector[0] + rotationVector[1] * rotationVector[1] + rotationVector[2] * rotationVector[2]);

    CGFloat angle = atan2(rotationLength / (startLength * currentLength), startDotCurrent / (startLength * currentLength));

    // normalize the rotation vector
    rotationVector[0] = rotationVector[0] / rotationLength;
    rotationVector[1] = rotationVector[1] / rotationLength;
    rotationVector[2] = rotationVector[2] / rotationLength;

    CATransform3D rotationTransform = CATransform3DMakeRotation(angle, rotationVector[0], rotationVector[1], rotationVector[2]);
    return CATransform3DConcat(baseTransform, rotationTransform);
}

提前致谢。

4

1 回答 1

2

看看我提出的一个问题......你可能正在尝试做同样的事情(我认为这个问题没有涵盖它,但是在旋转工作之后我实现了平移手势以允许用户旋转光盘方向)

如何在透视图中围绕其中心旋转平面对象?

于 2012-06-29T16:48:24.407 回答