旋转图像突然翻转

时间:2012-04-05 11:09:00

标签: iphone objective-c ios ios4

我使用以下代码通过在左右箭头上开始手势来旋转图像。沿着旋转移动箭头。

- (void)handleRotation:(UIPanGestureRecognizer *)recognizer 
{

UIView* cview = self.superview.superview;

UIView* lView = self.superview;

CGPoint origin = [lView convertPoint: self.center toView: cview]; 

if (recognizer.state == UIGestureRecognizerStateBegan)
{
    if (recognizer.view == leftImage)
    {
        initialP= [self convertPoint: leftImage.center toView: cview];
    }
    else
    {
        initialP = [self convertPoint: rightImage.center toView: cview];
    }
}
CGPoint currentP = [recognizer translationInView:cview];
currentP.x += initialP.x;
currentP.y += initialP.y;

CGFloat angle = 0;


CGFloat a = initialP.x - origin.x;
CGFloat b = initialP.y - origin.y;
CGFloat c = currentP.x - origin.x;
CGFloat d = currentP.y - origin.y;
angle =  acosf( ((a*c) + (b*d)) / ((sqrt(a*a + b*b)) * (sqrt(c*c + d*d))));


angle +=  lastReleasedAngle;

if (currentP.y < (currentP.x *(initialP.y - origin.y) + (initialP.x * origin.y - initialP.y * origin.x))  / (initialP.x - origin.x))
{
    angle = -angle;
}

if (recognizer.view == leftImage)
{
    angle = -angle;
}

if(recognizer.state == UIGestureRecognizerStateBegan || 
   recognizer.state == UIGestureRecognizerStateChanged || recognizer.state == UIGestureRecognizerStateEnded)
{

    [self setTransform: CGAffineTransformMakeRotation(angle)];
}  
if (recognizer.state == UIGestureRecognizerStateEnded)
{

    lastReleasedAngle = angle;   
}   

第一次开始旋转它完全正常工作。所以,右箭头现在是0度..当我从这里开始旋转并在这里释放它工作正常。当我在其他任何地方释放旋转然后尝试旋转,图像移动180度后开始精确到180度翻转。

1 个答案:

答案 0 :(得分:0)

在此处查看左侧,右侧旋转的示例,并依赖明确的位置download from here