You can animate the layer through one (or more) full rotations around its Z axis, animating the path to the transform.rotation
layer, for example:
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"]; animation.duration = .25; animation.fromValue = [NSNumber numberWithFloat:0]; animation.toValue = [NSNumber numberWithFloat:2 * M_PI]; [layer addAnimation:animation forKey:animation.keyPath];
You can animate around the X or Y axes using the transform.rotation.x
and transform.rotation.y
key paths. (The transform.rotation.z
key path has the same effect as the transform.rotation
key path.) You can apply multiple revolutions on separate axes at the same time.
Another way to do this, which probably works better if you want to rotate around an off-axis vector, uses keyframe animation, for example:
CALayer *layer = [sender layer]; CATransform3D transform = CATransform3DIdentity; transform.m34 = 1.0 / -50; layer.transform = transform; CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"transform"]; animation.values = [NSArray arrayWithObjects: [NSValue valueWithCATransform3D:CATransform3DRotate(transform, 0 * M_PI / 2, 100, 1, 100)], [NSValue valueWithCATransform3D:CATransform3DRotate(transform, 1 * M_PI / 2, 100, 1, 100)], [NSValue valueWithCATransform3D:CATransform3DRotate(transform, 2 * M_PI / 2, 100, 1, 100)], [NSValue valueWithCATransform3D:CATransform3DRotate(transform, 3 * M_PI / 2, 100, 1, 100)], [NSValue valueWithCATransform3D:CATransform3DRotate(transform, 4 * M_PI / 2, 100, 1, 100)], nil]; animation.duration = 2; [layer addAnimation:animation forKey:animation.keyPath];