1
votes

I am using the following code to rotate an image by starting gesture on left and right arrows.While moving arrows along the rotation.

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

First time when starts rotating it works perfectly fine.Say,right arrow is at 0 degree now..when i start rotation from here and release here it works fine.When i release the rotation anywhere else then tries to rotate ,image starts flipping exact to 180 degree after moving 180 degree.

1
Did you forgot a "=" in angle += lastReleasedAngle = angle;;?Manlio
yeah, my mistake while editing.user1226038

1 Answers

0
votes

check out the example over here of rotation with left ,right and depend on position wise download from here