2010-07-29 186 views
25

如何旋轉CALayer 90度?我需要旋轉一切包括子層和座標系。旋轉CALayer 90度?

+0

如果需要,不要忘記設置圖層的定位點。如果將它放在中間而不是圖層的頂部,它不會「翻轉」相同的方式 – guillaume 2013-05-27 13:01:27

回答

12

如果我製作動畫我用我的應用程序是這樣的:

- (NSObject *) defineZRotation { 
    // Define rotation on z axis 
    float degreesVariance = 90; 
    // object will always take shortest path, so that 
    // a rotation of less than 180 deg will move clockwise, and more than will move counterclockwise 
    float radiansToRotate = DegreesToRadians(degreesVariance); 
    CATransform3D zRotation; 
    zRotation = CATransform3DMakeRotation(radiansToRotate, 0, 0, 1.0); 
    // create an animation to hold "zRotation" transform 
    CABasicAnimation *animateZRotation; 
    animateZRotation = [CABasicAnimation animationWithKeyPath:@"transform"]; 
    // Assign "zRotation" to animation 
    animateZRotation.toValue = [NSValue valueWithCATransform3D:zRotation]; 
    // Duration, repeat count, etc 
    animateZRotation.duration = 1.5;//change this depending on your animation needs 
    // Here set cumulative, repeatCount, kCAFillMode, and others found in 
    // the CABasicAnimation Class Reference. 
    return animateZRotation; 
} 

當然,你可以在任何地方使用它,不要;噸需要從方法返回,如果那並不;噸套裝您的需求。

+1

動畫片很整齊。 – 2010-07-29 12:34:43

+1

很有幫助。祝你好運。 – Rab 2010-07-29 13:20:06

+0

無需創建用於旋轉視圖的CAAnimation對象。爲此,animateWithDuration:動畫效果很好。查看我的答案瞭解更多詳情。 – 2012-05-17 18:48:01

8

基本上類似的東西:

CGAffineTransform rotateTransform = CGAffineTransformMakeRotation(M_PI/2.0); [myCALayer setAffineTransform:rotateTransform];

編輯:它會順時針或依賴於平臺(iOS或Mac OS)上逆時針旋轉。

+3

弧度,而不是度數。 90.0/180.0 * M_PI或M_PI/2.0。 – 2010-07-29 12:03:00

+0

噢,對不起,沒有注意到。 – SteamTrout 2010-07-29 12:04:12

42

的OBJ-C:

theLayer.transform = CATransform3DMakeRotation(90.0/180.0 * M_PI, 0.0, 0.0, 1.0); 

夫特:

theLayer.transform = CATransform3DMakeRotation(90.0/180.0 * .pi, 0.0, 0.0, 1.0) 

即,變換使得其90度(π/ 2弧度)旋轉時,與100%的層旋轉發生在z軸周圍。

+1

M_PI是Double並且CATransform3DMakeRotation要求角度爲CGFloat,所以應該使用CGFloat(M_PI)代替 – 2015-08-04 15:26:40

+0

在C中,從double到CGFloat的轉換是隱式的(並且在C和Objective-C中轉換的形式爲*(T)表達式*,而不是* T(表達式)*。你是否想過Swift或C++?)另外,在上面的語句中,'M_PI'乘以'double',所以它被加寬,從而破壞了演員的目的。 – 2015-08-05 00:12:25

+0

對,我想到斯威夫特,在圓形路徑上旋轉圓弧時有困難。 – 2015-08-05 02:27:28

3

要旋轉90' 右:

myView.transform = CGAffineTransformMakeRotation(M_PI_2); 
+0

這是工作的意見不是圖層。 – anneblue 2015-08-07 14:53:13

+0

謝謝!它運作良好。 – Raja 2017-05-03 10:21:38

1

拉布展示瞭如何做一個CAAnimation對象做到這一點。它實際上簡單得多:

[myView animateWithDuration: 0.25 
    animations: 
    ^{ 
    myView.transform = CGAffineTransformMakeRotation(M_PI/2); 
    } 
]; 

(解除變換線從克里斯的回答 - 太懶惰,因爲他已經提供了完善的代碼重寫它。)

克里斯的代碼將旋轉視圖沒有動畫。我上面的代碼將與動畫一樣。

默認情況下,動畫使用容易,減少時間。你可以用一個稍微複雜一點的animateWithDuration調用來改變它(用animateWithDuration:delay:options:animations:completion:代替,並且在options參數中傳入所需的時間。)

+0

animateWithDuration:動畫:是UIView的基於塊的方法。這裏的問題是如何動畫CALayer - CALayer沒有這種方法,或者至少它沒有被記錄爲具有它。同樣在2010年,animateWithDuration:animations:方法還不存在(我認爲,在Obj-C中做任何基於塊的東西)......所以如果它適用的話,這將更像是「2012年更新」。 – Rab 2012-05-18 14:30:41

+0

也chris的代碼將動畫。與CALayer動畫的標準持續時間。 – vikingosegundo 2013-01-13 17:37:28