2012-06-29 34 views
0

嗨,我有一張像圓桌面的圖像。 當用戶從左向右滑動時,我想順時針移動它,當用戶從右向左滑動時,順時針移動它。使用iPad中的圖像進行動畫

就像實時移動圓桌面一樣。 我該如何在應用中做到這一點?

我正在使用下面的代碼進行旋轉。它來自TrackBall示例。 我遇到的問題是圖像旋轉的時候,它會改變它的位置。

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

在此先感謝。

+0

查找到兩個UIGestureRecognizer對象,CAAnimation(核心動畫)!否則,如果有特定的代碼問題,請發佈你已經有的東西! – trumpetlicks

+0

這是用於前端(網頁瀏覽,javascript)還是用於原生應用程序? –

+1

@Tom這是用於原生iPad應用程序。 – Shakti

回答