CATransform3DRotate rotates 360 degrees

I started using CATransform3D recently and it seems very enjoyable. Although I only have 1 problem with rotation. I am trying to turn my eyes 360 degrees to the right, but if I just put pass 360 in CATransform3DRotate it won't work (it just doesn't move at all.)

Here is my code:

CALayer *layer = dock.layer; CATransform3D r = CATransform3DIdentity; r.m34 = 1.0 / -500; r = CATransform3DRotate(r, DegreesToRadians(360.0f), 100.0f, 1.0f, 100.0f); layer.transform = r; 

Does anyone know how to solve this problem? Thanks in advance!:)

+6
source share
1 answer

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]; 
+22
source

Source: https://habr.com/ru/post/920931/


All Articles