2013-02-05 57 views
0

我有一個CGPath,我試圖旋轉,縮放和平移。我還有一個「可調整大小」的UIView,作爲用戶的助手,允許他/她應用轉換,所以無論何時此視圖的框架更改,新轉換應用於選定的CGPath。 另外,我將轉換錨點設置爲左上角。它縮放並旋轉得很好。但是,如果我設置的旋轉角度不是0,然後縮放,則錨點不再位於左上角。它看起來好像在旋轉過程中發生了變化,所以假設我們從0旋轉開始,一直到360,然後如我所期望的那樣將錨點設置回左上角。旋轉會影響縮放轉換錨點

下面是我使用創建的代碼轉換:

CGPoint anchorPointInPixels = CGPointMake(self.boundingBox.origin.x, self.boundingBox.origin.y); 

CGAffineTransform t = CGAffineTransformIdentity; 
t = CGAffineTransformTranslate(t, self.translation.x + anchorPointInPixels.x, self.translation.y + anchorPointInPixels.y); 
t = CGAffineTransformRotate(t, self.rotation); 
t = CGAffineTransformScale(t, self.scale.x, self.scale.y); 
t = CGAffineTransformTranslate(t, -anchorPointInPixels.x, -anchorPointInPixels.y); 
self.transform = t; 

讓我解釋一下這段代碼位: 1. Path的點是絕對座標 2.邊界框計算只是一次,它被設置爲包圍路徑中所有點的矩形。邊界框也在絕對座標 3.翻譯指定從邊界框的原點偏移,所以當路徑已創建時,翻譯等於0,它仍然是這樣,直到用戶移動它

所以,如何讓它旋轉,而不影響錨點?

感謝您的閱讀!

馬里亞諾

回答

0

所以我能夠通過連接矩陣來解決這個問題。下面的代碼看起來像:

CGPoint anchorPointForScalingInPixels = CGPointMake(origin.x + size.width * self.anchorPointForScaling.x, 
                origin.y + size.height * self.anchorPointForScaling.y); 

CGPoint anchorPointForRotationInPixels = CGPointMake(origin.x + size.width * self.anchorPointForRotation.x, 
                origin.y + size.height * self.anchorPointForRotation.y); 

CGAffineTransform rotation = CGAffineTransformIdentity; 
rotation = CGAffineTransformTranslate(rotation, anchorPointForRotationInPixels.x, anchorPointForRotationInPixels.y); 
rotation = CGAffineTransformRotate(rotation, self.rotation); 
rotation = CGAffineTransformTranslate(rotation, -anchorPointForRotationInPixels.x, -anchorPointForRotationInPixels.y); 

CGAffineTransform scale = CGAffineTransformIdentity; 
scale = CGAffineTransformTranslate(scale, anchorPointForScalingInPixels.x, anchorPointForScalingInPixels.y); 
scale = CGAffineTransformScale(scale, self.scale.x, self.scale.y); 
scale = CGAffineTransformTranslate(scale, -anchorPointForScalingInPixels.x, -anchorPointForScalingInPixels.y); 

CGAffineTransform translate = CGAffineTransformMakeTranslation(self.translation.x, self.translation.y); 

CGAffineTransform t = CGAffineTransformConcat(rotation, CGAffineTransformConcat(scale, translate)); 

這樣我可以處理兩個錨點,一個旋轉,另一個用於縮放。