2011-02-03 94 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