0

我需要在 iPad 应用程序中旋转图像(例如顺时针)。图像保存在视图中:

UIImageView *view1 = [[UIImageView alloc] initWithFrame:CGRectMake(startX+lineWidth, startY+lineHeight, lineWidth, lineHeight)];
view1.tag = 0;
view1.image = [UIImage imageNamed:[images objectAtIndex:0]];

- (void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event

我正在这样做:

CGPoint d1 = [touch locationInView:touch.view];
CGPoint d2 = [touch previousLocationInView:touch.view];               
CGFloat angle1 = atan2(d1.y, d1.x);
CGFloat angle2 = atan2(d2.y, d2.x);
subview.transform = CGAffineTransformRotate(subview.transform, angle2-angle1);

视图正在旋转,但它与触摸(或我的开发环境中的光标)一起运行并不顺畅。相反,它在触摸之前浮动一点。

我错过了什么?

4

1 回答 1

0

您为什么不UIRotationGestureRecognizer为此使用 an(请参阅Apple 文档中的事件处理编程指南)?

它将使用标准旋转手势(两根手指)处理旋转,并直接为您提供角度和角度,并且更易于使用。

但是要直接回答您的问题,您的问题可能是您没有累积角度。CGAffineTransformRotate您可以使用angle2-angle1当前触摸和上一次触摸之间的角度差来应用变换。所以每次你移动你的手指,你改变的旋转subview角度只是前一个点和当前点之间的差异,而不是触摸开始时的起点和当前点之间的差异。您应该使用在touchesBegan:withEvent:ford2而不是检索到的点previousLocationInView:

于 2012-11-20T13:48:21.583 回答