2010-10-13 12 views

Respuesta

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

Dónde center es el centro de su capa, rotationAngle es en radianes (positiva es hacia la izquierda), y rotationPoint es el punto sobre el cual desea rotar center y rotationPoint están en el espacio de coordenadas de la vista que contiene.

+0

Gracias warrenm funciona ....... –

+0

¡Has salvado mis días! ¡¡Gracias!! –

1

Consulte la documentación de CA here.

Desea establecer la transformación a un CATransform3DRotate, por ejemplo:

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

Esto sólo será suficiente para la rotación de la capa alrededor de su centro actual, no una arbitraria punto. – warrenm

2

enter image description here

  1. Definir su subcapa que desea girar;
  2. Establezca su bounds, position en superlayer y anchorPoint. anchorPoint tiene coordenadas relativas y apunta a anchorPoint. Su subcapa girará alrededor de este anchorPoint;
  3. Añadir transformación.

Por ejemplo, para girar una subcapa en su punto central superior supervista alrededor de la parte inferior central de subcapa, utilizar este código:

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