- How To Rotate an UIImageView based on a point in Touches Moved Method?
- CGAffineTransform transforms = CGAffineTransformMakeRotation(M_PI/2);
- imgView.transform = transforms;
- CGAffineTransform transforms = CGAffineTransformRotate(imgView.transform, M_PI/2);
- imgView.transform = transforms;
- -(void)transformImagewithTouches:(UITouch *)touchLocation
- {
- //NSLog(@"Before %f",self.fltRotatedAngle);
- CGPoint touchLocationpoint = [touchLocation locationInView:[self superview]];
- CGPoint PrevioustouchLocationpoint = [touchLocation previousLocationInView:[self superview]];
- CGPoint origin;
- origin.x=self.center.x;
- origin.y=self.center.y;
- CGPoint previousDifference = [self vectorFromPoint:origin toPoint:PrevioustouchLocationpoint];
- CGAffineTransform newTransform =CGAffineTransformScale(self.transform, 1, 1);
- CGFloat previousRotation = atan2(previousDifference.y, previousDifference.x);
- CGPoint currentDifference = [self vectorFromPoint:origin toPoint:touchLocationpoint];
- CGFloat currentRotation = atan2(currentDifference.y, currentDifference.x);
- CGFloat newAngle = currentRotation- previousRotation;
- //Calculate Angle to Store
- fltTmpAngle = fltTmpAngle+newAngle;
- self.fltRotatedAngle = (fltTmpAngle*180)/M_PI;
- newTransform = CGAffineTransformRotate(newTransform, newAngle);
- [self animateImageView:self toPosition:newTransform];
- }
- -(void)animateImageView:(UIImageView *)theView toPosition:(CGAffineTransform) newTransform
- {
- [UIView setAnimationsEnabled:YES];
- [UIView beginAnimations:nil context:NULL];
- [UIView setAnimationCurve:UIViewAnimationCurveLinear];
- [UIView setAnimationBeginsFromCurrentState:YES];
- [UIView setAnimationDuration:0.0750];
- self.transform = newTransform;
- [UIView commitAnimations];
- }
- -(CGPoint)vectorFromPoint:(CGPoint)firstPoint toPoint:(CGPoint)secondPoint
- {
- CGFloat x = secondPoint.x - firstPoint.x;
- CGFloat y = secondPoint.y - firstPoint.y;
- CGPoint result = CGPointMake(x, y);
- return result;
- }
- CGAffineTransform transforms = CGAffineTransformConcat(imgView.transform,CGAffineTransformMakeRotation(M_PI/2));
- imgView.transform = transforms;
- pCalculator.tangentToCornerAngle is the desired angle in which rotation has to be made.
- Function is CATransform3DMakeRotation(CGFloat angle, CGFloat x, <CGFloat y, CGFloat z);