2010-10-13 9 views

risposta

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

Dove center è il centro del vostro livello, rotationAngle è in radianti (positivo è in senso antiorario), e rotationPoint è la punto su cui si desidera ruotare. center e rotationPoint si trovano nello spazio delle coordinate della vista contenente.

+0

Grazie warrenm suo lavoro ....... –

+0

Hai salvato i miei giorni! Grazie!! –

1

Controllare la documentazione CA here.

Si desidera impostare la trasformazione a un CATransform3DRotate, ad esempio:

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

Questo sarà sufficiente solo per ruotare lo strato attorno al suo centro corrente, non un arbitrario punto. – warrenm

2

enter image description here

  1. Definire il sottolivello che si desidera ruotare;
  2. Impostare il suo bounds, position in superlayer e anchorPoint. anchorPoint ha le coordinate relative e punta a un anchorPoint. Il sottostrato ruoterà attorno a questo anchorPoint;
  3. Aggiungi trasformazione.

Ad esempio, per ruotare un substrato sul suo punto superview centrale superiore intorno al centro inferiore del substrato, di questo codice:

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