2011-05-11 64 views
0

亲爱的,如何根据触摸移动方法中的点旋转UIImageView?

我想相对于touchesMoved方法的一个点一个UIImageView旋转。

我试过2 -3种方法,但我没有得到我期望的确切结果。

首先方法我用

CGAffineTransform transforms = CGAffineTransformMakeRotation(M_PI/2); 

imgView.transform = transforms; 

这是写在touchesMoved。所以我期望在每个touchesMoved中的图像视图旋转。 但是旋转只发生一次。

我用第二种方法

CGAffineTransform transforms = CGAffineTransformRotate(imgView.transform, M_PI/2); 

imgView.transform = transforms; 

现在的结果是我得到的是在图像视图图像中的每个举动continusely旋转。但imageview不旋转。我需要的是旋转im​​ageview而不是图像。

任何帮助将不胜感激。

感谢&最好的问候, Rupesh [R梅农

+0

难道ü尝试CABasicAnimation? – Krishnan 2011-05-11 10:35:57

+0

是的。它也没有给出所需的输出。 – 2011-05-11 11:17:28

回答

0

那是因为你已经通过M_PI/2旋转它,当你再次旋转它,它从初始位置转动它。如果我理解正确,您想达到的图像上单指旋转利用这一

CGAffineTransform transforms = CGAffineTransformConcat(imgView.transform,CGAffineTransformMakeRotation(M_PI/2)); 
imgView.transform = transforms; 
+0

亲爱的Inder,我尝试了你的建议。但它给了我与第二种方法一样的结果。即;图像正在旋转而不是图像视图。我需要图像视图旋转。任何如何感谢所显示的兴趣。 – 2011-05-11 11:08:19

+0

你会怎么说imageView没有被旋转,你正在旋转imageView没有图像,为什么内容旋转... – 2011-05-11 11:40:26

+0

我亲爱的朋友,你必须有一个方形imageView旋转它90度,使它感觉一样。要么改变其高度或宽度(一个矩形不是一个正方形),那么你可以看到不同之处,或者你可以将它旋转45度,仅用于测试目的。 'M_PI/4' – 2011-05-11 11:59:06

1

。如果是这样,您可以使用我的一个现场工作项目中的以下功能。您可以将其用于单张图像以及多张图像。您需要在某种程度上修改多个图像。最好的方法是扩展UIImageView类并创建自己的类。

从触摸移动调用此函数

  • [自transformImagewithTouches:触摸];

声明1属性并合成如下。 (也请如果需要在下面的代码声明其他变量

  • @属性(非原子)CGFloat的fltRotatedAngle;
-(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; 
} 

希望它可以帮助如果你卡住了,请让我。知道我一定会帮你的

+0

谢谢Jennnis,这真的很有帮助。我能够使用代码中的某个部分。 – 2011-05-12 03:48:34

+0

@ Jennis,我想要旋转以及翻译,如果我想旋转,然后将我的图像拖到屏幕的其他部分,我该怎么做? – user930195 2011-11-12 08:10:04

+0

您必须首先定义动作,即旋转或翻译的内容。并根据选定的行动相应采取行动。您可以从教程中轻松获得翻译代码。和我已经给出的轮换。 – 2011-11-12 12:55:35

1

感谢您对我的帮助所表示的兴趣

当我使用下面的代码时,我能够以所需的角度旋转图像视图。

imgView.layer。transform = CATransform3DMakeRotation(pCalculator.tangentToCornerAngle,0,0,1);

其中img查看是一个UIImageView。

pCalculator.tangentToCornerAngle is the desired angle in which rotation has to be made. 

Function is CATransform3DMakeRotation(CGFloat angle, CGFloat x, <CGFloat y, CGFloat z); 

三江源所有, Rupesh [R梅农