2010-10-13 128 views

回答

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是点你想旋转。 centerrotationPoint位于包含视图的坐标空间中。

+0

感谢warrenm其工作....... – 2010-10-14 11:37:37

+0

你救了我的日子!谢谢!! – 2013-04-26 21:29:28

1

查看CA文档here

你想设置的转换为CATransform3DRotate,例如:

CATransform3D current = myLayer.transform; 
myLayer.transform = CATransform3DRotate(current, DEGREES_TO_RADIANS(20), 0, 1.0, 0); 
+0

这将只足够用于层关于其当前中心,而不是任意的旋转点。 – warrenm 2010-10-14 02:47:59

2

enter image description here

  1. 定义要旋转你的子层;
  2. 设置它的bounds,position在超级层和anchorPointanchorPoint有相对坐标并且指向anchorPoint。您的子图层将围绕此旋转anchorPoint;
  3. 添加转换。

例如,为了对围绕子层的底部中心它的父顶部中心点旋转的子层,使用以下代码:

// 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)