title: How to measure velocity of a swipe in iPhone and then calculate the rotation of an image?
body: <p>I have used following code and I can rotate my image. But after animation I don't know how to get the final angle of an image.</p>
  
  <p>I want to calculate <strong>Rotation angle</strong> of an image after "touch ended" event.</p>
  
  <pre><code>- (void)rotateAccordingToAngle:(float)angle
  {
      [NumbersImage setTransform:CGAffineTransformRotate(NumbersImage.transform, angle)];  
  }
  - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
  {    
      [NumbersImage.layer removeAllAnimations];
      previousTimestamp = event.timestamp;
  }
  - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
  {
      UITouch *touch = [touches anyObject];
      if ([touch view] == NumbersImage) {
  
      CGPoint center = CGPointMake(CGRectGetMidX([NumbersImage bounds]), CGRectGetMidY([NumbersImage bounds]));
      CGPoint currentTouchPoint = [touch locationInView:NumbersImage];
      CGPoint previousTouchPoint = [touch previousLocationInView:NumbersImage];
      CGFloat angleInRadians = atan2f(currentTouchPoint.y - center.y, currentTouchPoint.x - center.x) - atan2f(previousTouchPoint.y - center.y, previousTouchPoint.x - center.x);
  
      [self rotateAccordingToAngle:angleInRadians];
  
      CGFloat angleInDegree = RadiansToDegrees(angleInRadians);
      FinalDegree +=angleInDegree;        
      revolutions+= (angleInDegree/360.0f);
  }
  
  }
  - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
  {
      UITouch *touch = [touches anyObject];
  
  if ([touch view] == NumbersImage) {
  
      NSTimeInterval timeSincePrevious = event.timestamp - previousTimestamp;
      CGFloat revolutionsPerSecond = revolutions/timeSincePrevious;
  
      [self startAnimationWithRevolutions:revolutionsPerSecond forTime:5.0f];
      NSLog(@"Revolution per second = %f",revolutionsPerSecond);
  
      revolutions = 0;
  }
  }
  
  CGFloat RadiansToDegrees(CGFloat radians)
  {
      //NSLog(@"Radians %f",radians);
      return radians * 180 / M_PI;
  };
  
  
  - (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag
  {
  NumbersImage.userInteractionEnabled = TRUE;
      if (timerUpdate) {
          [timerUpdate invalidate];
          timerUpdate = nil;
      }
  }
  -(void)updateTransform{
      NumbersImage.transform = [[NumbersImage.layer presentationLayer] affineTransform];
  }
  -(void)startAnimationWithRevolutions:(float)revPerSecond forTime:(float)time
  {
      NumbersImage.userInteractionEnabled = FALSE;
      float totalRevolutions = revPerSecond * time;
  
      timerUpdate = [NSTimer scheduledTimerWithTimeInterval:0.1f target:self selector:@selector(updateTransform) userInfo:nil repeats:YES];
  
      [CATransaction begin];
      [CATransaction setValue:[NSNumber numberWithFloat:time]forKey:kCATransactionAnimationDuration];
  
      CABasicAnimation* spinAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
  CGAffineTransform transform = NumbersImage.transform;
  
  float fromAngle = atan2(transform.b, transform.a);
  float toAngle = fromAngle + (totalRevolutions*4*M_PI);
  
  NSLog(@"To Angle = %f",toAngle);
  NSLog(@"From Angle = %f",fromAngle);
  spinAnimation.fromValue = [NSNumber numberWithFloat:fromAngle];
  spinAnimation.toValue = [NSNumber numberWithFloat:toAngle];
  spinAnimation.repeatCount = 0;
  spinAnimation.removedOnCompletion = NO;
  spinAnimation.delegate = self;
  spinAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
  [NumbersImage.layer addAnimation:spinAnimation forKey:@"spinAnimation"];
  [CATransaction commit];
  
  
  //NSLog(@"Total Rotation = %f",totalRevolutions);
  
  //NSLog(@"Degree Rotation %f", temp);
  
  //[self Rotate];
  CGFloat temp = RadiansToDegrees(fromAngle);
  FinalDegree += temp;
  //NSLog(@"FinalDegree %f",FinalDegree);
  
  
  //NSLog(@"Final degree %f",FinalDegree);
  //NSLog(@"Total %f",total);
  
  }
  
  CGFloat DegreesToRadians(CGFloat degrees)
  {
      return degrees * M_PI / 180;
  }
  </code></pre>
  
  <p>This code is used to measure velocity and rotate image as per user touch.</p>
  
  <p>But I am not able to get the final angle of the image.</p>
  
  <p>Please somebody help me in this code.</p>
  
  <p>Thanks,</p>
  
  <p>Hemang.</p>