最简单的方法是使用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;
查看维基百科或做一个谷歌搜索更多地了解极坐标,以及如何直角坐标和极之间的转换坐标系统。
感谢您的回答。不幸的是,手势不能为我提供我喜欢的用户交互体验。例如,手势无法增加宽度并固定视图的高度。此外,如果我使用手势,我将无法使用单个手指移动叠加层。 – adranale 2011-02-03 15:54:34