2011-02-03 125 views
1

我有一个覆盖视图(其中有自绘形状),我在ImageView上显示。我希望视图可以移动,可调整大小和可旋转。我可以允许用户通过从中间拖动它来移动覆盖图,或者通过从两侧(右侧或底部)中的一个拖动它来调整它的大小。我仍然不能做的是让用户通过移动左上边缘来旋转它。在iphone中旋转覆盖视图

myView.transform = CGAffineTransformMakeRotation(angle * M_PI/180); 

但我怎么能够基于用户触摸角度?有任何想法吗?

回答

2

最简单的方法是使用UIRotationGestureRecognizer,它将旋转值作为属性。

如果您不能使用手势识别,尝试这样的事情(未经测试):

// Assuming centerPoint is the center point of the object you want to rotate (the rotation axis), 
// currentTouchLocation and initialTouchLocation are the coordinates of the points between 
// which you want to calculate the angle. 
CGPoint centerPoint = ... 
CGPoint currentTouchLocation = ... 
CGPoint initialTouchLocation = ... 

// Convert to polar coordinates with the centerPoint being (0,0) 
CGPoint currentTouchLocationNormalized = CGPointMake(currentTouchLocation.x - centerPoint.x, currentTouchLocation.y - centerPoint.y); 
CGPoint initialTouchLocationNormalized = CGPointMake(initialTouchLocation.x - centerPoint.x, initialTouchLocation.y - centerPoint.y); 

CGFloat angleBetweenInitialTouchAndCenter = atan2(initialTouchLocationNormalized.y, initialTouchLocationNormalized.x); 
CGFloat angleBetweenCurrentTouchAndCenter = atan2(currentTouchLocationNormalized.y, currentTouchLocationNormalized.x); 

CGFloat rotationAngle = angleBetweenCurrentTouchAndCenter - angleBetweenInitialTouchAndCenter; 

查看维基百科或做一个谷歌搜索更多地了解极坐标,以及如何直角坐标和极之间的转换坐标系统。

+0

感谢您的回答。不幸的是,手势不能为我提供我喜欢的用户交互体验。例如,手势无法增加宽度并固定视图的高度。此外,如果我使用手势,我将无法使用单个手指移动叠加层。 – adranale 2011-02-03 15:54:34