如何在一個選定點上旋轉CALayer。如何在一個點上旋轉CALayer
18
A
回答
34
CATransform3D transform = CATransform3DIdentity;
transform = CATransform3DTranslate(transform, rotationPoint.x-center.x, rotationPoint.y-center.y, 0.0);
transform = CATransform3DRotate(transform, rotationAngle, 0.0, 0.0, -1.0);
transform = CATransform3DTranslate(transform, center.x-rotationPoint.x, center.y-rotationPoint.y, 0.0);
哪裏center
是您層的中心,rotationAngle
是弧度(正面是逆時針),並且rotationPoint
是點你想旋轉。 center
和rotationPoint
位於包含視圖的座標空間中。
1
2
- 定義要旋轉你的子層;
- 設置它的
bounds
,position
在超級層和anchorPoint
。anchorPoint
有相對座標並且指向anchorPoint
。您的子圖層將圍繞此旋轉anchorPoint
; - 添加轉換。
例如,爲了對圍繞子層的底部中心它的父頂部中心點旋轉的子層,使用以下代碼:
// 1
let rect = CGRect(x: 0,
y: 0,
width: 20,
height: 20)
let path = UIBezierPath(rect: rect)
let sublayer = CAShapeLayer()
sublayer.fillColor = UIColor.green.cgColor
sublayer.path = path.cgPath
superlayer.addSublayer(sublayer)
// 2
sublayer.bounds = rect
sublayer.position = CGPoint(x: superlayer.bounds.size.width/2, y: 0)
sublayer.anchorPoint = CGPoint(x: 0.5, y: 1)
// 3
let rotationAnimation = CABasicAnimation(keyPath: "transform.rotation.z")
rotationAnimation.toValue = 2*CGFloat.pi
rotationAnimation.duration = 3
rotationAnimation.fillMode = kCAFillModeForwards
rotationAnimation.isRemovedOnCompletion = false
sublayer.add(rotationAnimation, forKey: nil)
相關問題
- 1. CALayer的重繪上旋轉
- 2. 如何在當前位置旋轉CALayer?
- 3. 旋轉CALayer 90度?
- 4. 如何在旋轉後找到圖像上的一個點?
- 5. 如何在中心點上旋轉一個android圖標?
- 6. 如何讓相機在一個點上旋轉?
- 7. CALayer圍繞中心旋轉
- 8. 旋轉NSImageView的CALayer 90度
- 9. 如何轉換(旋轉)已存在的CALayer /動畫?
- 10. OpenGL旋轉一個點
- 11. 圍繞一個點旋轉
- 12. 圍繞另一點旋轉一個點
- 13. 在圖像上設置一個點作爲旋轉點Pygame.transform.rotate()
- 14. 如何計算旋轉軸上的點?
- 15. 如何跟蹤旋轉MovieClip上的點?
- 16. 在HTML畫布中旋轉一個點
- 17. 如何在任何角度旋轉一個點?
- 18. 如何永久性地讓CALayer在最終位置旋轉?
- 19. 如何將一個精靈旋轉到一個點?
- 20. 如何旋轉一個UIView來面對一個點
- 21. 強制CALayer的旋轉方向?
- 22. 旋轉UIView,包括CALayer界限和CALayer框架
- 23. 在一個方向上旋轉矩陣
- 24. 2D陣列旋轉和旋轉vb.net中的一個點
- 25. 飛機上的旋轉點
- 26. 如何旋轉CALayer支持的NSView子類
- 27. 如何拖動旋轉和縮放的CALayer?
- 28. 如何旋轉UIView,UIImageView或CALayer動畫360˚?
- 29. 旋轉90度後,如何將圖像添加到CALayer背面?
- 30. 僅圍繞一個軸旋轉節點
感謝warrenm其工作....... – 2010-10-14 11:37:37
你救了我的日子!謝謝!! – 2013-04-26 21:29:28