最簡單的方法是使用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