【问题标题】:How To Rotate an UIImageView based on a point in Touches Moved Method?如何根据 Touches Moved 方法中的点旋转 UIImageView?
【发布时间】:2011-05-11 10:22:06
【问题描述】:

亲爱的,

我想在 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;

现在我得到的结果是图像视图中的图像在每次移动中不断旋转。但是 imageview 没有旋转。我需要的是旋转图像视图而不是图像。

任何帮助将不胜感激。

感谢和最好的问候, 鲁佩什 R 梅农

【问题讨论】:

  • 是的。它也没有提供所需的输出。

标签: iphone objective-c ipad


【解决方案1】:

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

从触摸移动调用这个函数

  • [self transformImagewithTouches:touch];

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

  • @property (nonatomic) 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;
}

希望对您有所帮助。如果你坚持,请告诉我,我一定会帮助你。

【讨论】:

  • 谢谢 Jennnis,这真的很有帮助。我能够使用您代码中的某些部分。
  • @Jennis,如果我想旋转然后将我的图像拖到屏幕的其他部分,我想要旋转和平移,我该怎么做?
  • 您必须首先定义操作,即如何进行旋转或平移。并根据选定的行动采取相应的行动。您可以从教程中轻松获取翻译代码。和我给出的轮换。
【解决方案2】:

感谢您对我的帮助。

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

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

其中 imgView 是 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);

谢谢大家, 鲁佩什·R·梅农

【讨论】:

    【解决方案3】:

    这是因为你已经将它旋转了 M_PI/2,当你再次旋转它时,它会从初始位置旋转它。使用这个

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

    【讨论】:

    • 亲爱的 Inder,我已经尝试过您的建议。但它给了我与第二种方法相同的结果。 IE;图像正在旋转而不是图像视图。我需要旋转图像视图。无论如何感谢您的关注。
    • 你怎么说 imageView 没有被旋转,你正在旋转 imageView 没有为什么内容被旋转的图像......
    • 亲爱的朋友,你一定有一个方形的 imageView,所以将它旋转 90 度让它感觉一样。要么改变它的高度或宽度(一个矩形而不是一个正方形)然后你可以看到差异,或者你可以将它旋转 45 度仅用于测试目的。 M_PI/4
    • 不,亲爱的。我的图像视图不是方形的。它的纯矩形。
    • 但是已经在一个测试项目中对其进行了测试,它工作正常......只需将背景颜色更改为红色并在其中放置一个较小的图像并将其 contentMode 设置为居中
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多