如何旋轉CALayer 90度?我需要旋轉一切包括子層和座標系。旋轉CALayer 90度?
回答
如果我製作動畫我用我的應用程序是這樣的:
- (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;
}
當然,你可以在任何地方使用它,不要;噸需要從方法返回,如果那並不;噸套裝您的需求。
動畫片很整齊。 – 2010-07-29 12:34:43
很有幫助。祝你好運。 – Rab 2010-07-29 13:20:06
無需創建用於旋轉視圖的CAAnimation對象。爲此,animateWithDuration:動畫效果很好。查看我的答案瞭解更多詳情。 – 2012-05-17 18:48:01
基本上類似的東西:
CGAffineTransform rotateTransform = CGAffineTransformMakeRotation(M_PI/2.0); [myCALayer setAffineTransform:rotateTransform];
編輯:它會順時針或依賴於平臺(iOS或Mac OS)上逆時針旋轉。
弧度,而不是度數。 90.0/180.0 * M_PI或M_PI/2.0。 – 2010-07-29 12:03:00
噢,對不起,沒有注意到。 – SteamTrout 2010-07-29 12:04:12
的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軸周圍。
M_PI是Double並且CATransform3DMakeRotation要求角度爲CGFloat,所以應該使用CGFloat(M_PI)代替 – 2015-08-04 15:26:40
在C中,從double到CGFloat的轉換是隱式的(並且在C和Objective-C中轉換的形式爲*(T)表達式*,而不是* T(表達式)*。你是否想過Swift或C++?)另外,在上面的語句中,'M_PI'乘以'double',所以它被加寬,從而破壞了演員的目的。 – 2015-08-05 00:12:25
對,我想到斯威夫特,在圓形路徑上旋轉圓弧時有困難。 – 2015-08-05 02:27:28
拉布展示瞭如何做一個CAAnimation
對象做到這一點。它實際上簡單得多:
[myView animateWithDuration: 0.25
animations:
^{
myView.transform = CGAffineTransformMakeRotation(M_PI/2);
}
];
(解除變換線從克里斯的回答 - 太懶惰,因爲他已經提供了完善的代碼重寫它。)
克里斯的代碼將旋轉視圖沒有動畫。我上面的代碼將與動畫一樣。
默認情況下,動畫使用容易,減少時間。你可以用一個稍微複雜一點的animateWithDuration
調用來改變它(用animateWithDuration:delay:options:animations:completion:
代替,並且在options參數中傳入所需的時間。)
animateWithDuration:動畫:是UIView的基於塊的方法。這裏的問題是如何動畫CALayer - CALayer沒有這種方法,或者至少它沒有被記錄爲具有它。同樣在2010年,animateWithDuration:animations:方法還不存在(我認爲,在Obj-C中做任何基於塊的東西)......所以如果它適用的話,這將更像是「2012年更新」。 – Rab 2012-05-18 14:30:41
也chris的代碼將動畫。與CALayer動畫的標準持續時間。 – vikingosegundo 2013-01-13 17:37:28
- 1. 旋轉NSImageView的CALayer 90度
- 2. 旋轉90度後,如何將圖像添加到CALayer背面?
- 3. 圓球90度旋轉
- 4. 如何旋轉TeeChart 90度
- 5. 旋轉GoJs形狀90度
- 6. 矩形旋轉90度libgdx
- 7. 旋轉(90度)的ViewGroup根
- 8. 子報表旋轉90度
- 9. Excel旋轉(不轉置)表順時針旋轉90度
- 10. 旋轉視圖層次結構90度
- 11. 在Chrome中旋轉90度的圖像
- 12. 與曲線旋轉matplotlib pyplot 90度
- 13. Android - 旋轉Admob視圖90度?
- 14. android按鈕右對齊旋轉-90度
- 15. 表中的90度文字旋轉
- 16. 瞭解如何旋轉矩陣90度
- 17. 垂直對齊90度旋轉文字
- 18. 使用GDI將文本旋轉90度
- 19. 旋轉文字/格90度(靜態)
- 20. Java逆時針旋轉90度
- 21. 順時針旋轉2d向量90度
- 22. UIImagePNGRepresentation問題? /圖像旋轉90度
- 23. Android Camera2預覽偶爾旋轉90度
- 24. 如何順時針旋轉90度(swift3)
- 25. 順時針旋轉90度的圖像
- 26. 如何將輸出旋轉90度?
- 27. 如何旋轉90度的UIImage?
- 28. STemWin STM32F429發現屏幕旋轉90度
- 29. iPhone屏幕旋轉:我可以強制旋轉90度嗎?
- 30. CALayer的重繪上旋轉
如果需要,不要忘記設置圖層的定位點。如果將它放在中間而不是圖層的頂部,它不會「翻轉」相同的方式 – guillaume 2013-05-27 13:01:27