Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 17th, 2012  |  syntax: None  |  size: 2.33 KB  |  hits: 18  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. How To Rotate an UIImageView based on a point in Touches Moved Method?
  2. CGAffineTransform transforms = CGAffineTransformMakeRotation(M_PI/2);
  3.  
  4. imgView.transform = transforms;
  5.        
  6. CGAffineTransform transforms = CGAffineTransformRotate(imgView.transform, M_PI/2);
  7.  
  8. imgView.transform = transforms;
  9.        
  10. -(void)transformImagewithTouches:(UITouch *)touchLocation
  11. {
  12.     //NSLog(@"Before %f",self.fltRotatedAngle);
  13.     CGPoint touchLocationpoint = [touchLocation locationInView:[self superview]];
  14.     CGPoint PrevioustouchLocationpoint = [touchLocation previousLocationInView:[self superview]];
  15.     CGPoint origin;
  16.     origin.x=self.center.x;
  17.     origin.y=self.center.y;
  18.     CGPoint previousDifference = [self vectorFromPoint:origin toPoint:PrevioustouchLocationpoint];
  19.     CGAffineTransform newTransform =CGAffineTransformScale(self.transform, 1, 1);
  20.     CGFloat previousRotation = atan2(previousDifference.y, previousDifference.x);
  21.     CGPoint currentDifference = [self vectorFromPoint:origin toPoint:touchLocationpoint];
  22.     CGFloat currentRotation = atan2(currentDifference.y, currentDifference.x);
  23.     CGFloat newAngle = currentRotation- previousRotation;
  24.  
  25.     //Calculate Angle to Store
  26.     fltTmpAngle = fltTmpAngle+newAngle;
  27.     self.fltRotatedAngle = (fltTmpAngle*180)/M_PI;  
  28.  
  29.     newTransform = CGAffineTransformRotate(newTransform, newAngle);
  30.     [self animateImageView:self toPosition:newTransform];  
  31. }
  32.  
  33. -(void)animateImageView:(UIImageView *)theView toPosition:(CGAffineTransform) newTransform
  34. {
  35.     [UIView setAnimationsEnabled:YES];
  36.     [UIView beginAnimations:nil context:NULL];
  37.     [UIView setAnimationCurve:UIViewAnimationCurveLinear];
  38.     [UIView setAnimationBeginsFromCurrentState:YES];
  39.     [UIView setAnimationDuration:0.0750];  
  40.     self.transform = newTransform;
  41.     [UIView commitAnimations];
  42. }
  43.  
  44. -(CGPoint)vectorFromPoint:(CGPoint)firstPoint toPoint:(CGPoint)secondPoint
  45. {
  46.     CGFloat x = secondPoint.x - firstPoint.x;
  47.     CGFloat y = secondPoint.y - firstPoint.y;
  48.     CGPoint result = CGPointMake(x, y);
  49.     return result;
  50. }
  51.        
  52. CGAffineTransform transforms = CGAffineTransformConcat(imgView.transform,CGAffineTransformMakeRotation(M_PI/2));
  53. imgView.transform = transforms;
  54.        
  55. pCalculator.tangentToCornerAngle  is the desired angle in which rotation has to be made.
  56.  
  57. Function is CATransform3DMakeRotation(CGFloat angle, CGFloat x, <CGFloat y, CGFloat z);