2011-02-19 44 views
19

我正在使用以下代碼來旋轉圖像,但已經旋轉出「頁面」的圖像的一半(向下y軸)消失。怎麼修? heading以弧度表示。CATransform3D旋轉會導致圖像的一半消失

CALayer *layer = myUIImageView.layer; 
    CATransform3D rotationAndPerspectiveTransform = CATransform3DIdentity; 
    rotationAndPerspectiveTransform.m34 = 1.0/500; 
    rotationAndPerspectiveTransform = CATransform3DRotate(rotationAndPerspectiveTransform, heading, 0.0f, 1.0f, 0.0f); 
    layer.transform = rotationAndPerspectiveTransform; 
+0

事實上,這似乎惹的z-index。這個UIImageView是UIView的一部分,它部分地位於另一個UIView的「後面」,但是在旋轉之後,myUIImageView總是繪製在頂部。 – iPadDeveloper2011 2011-02-19 06:13:05

回答

32

解決方法是適當地設置我所有圖層的zPosition屬性。感謝@Brad Larson,他在評論here中提出了此解決方案。看起來,當您開始使用CATransform3D時,由addsubview建立的普通zindex視圖層次被拋出窗口。

5
layer.anchorPoint = CGPointMake(0.0, 0.0); 
0

設置anchorPoint{0.0, 0.0}作品,以及(如利亞姆已經指出的那樣)。

下面是改變anchorPoint不改變屏幕上的層的位置,完整的代碼片段:

 layer.anchorPoint = CGPointMake(0.0, 0.0); 

    CGPoint position = layer.position; 
    CGSize size = layer.bounds.size; 

    CGFloat posX = position.x - size.width/2.0; 
    CGFloat posY = position.y - size.height/2.0; 

    layer.position = CGPointMake(posX, posY);